blob: 9327bb78c4658e9aae868005dd3fcdde1ae141bb [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()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000302 OutStreamer.EmitRawText(StringRef("\t.code\t16"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000303 if (!Subtarget->isTargetDarwin())
Chris Lattner9d7efd32010-04-04 07:05:53 +0000304 OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000305 else {
306 // This needs to emit to a temporary string to get properly quoted
307 // MCSymbols when they have spaces in them.
308 SmallString<128> Tmp;
309 raw_svector_ostream OS(Tmp);
310 OS << "\t.thumb_func\t" << *CurrentFnSym;
311 OutStreamer.EmitRawText(OS.str());
312 }
Chris Lattner953ebb72010-01-27 23:58:11 +0000313 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000314
Chris Lattner953ebb72010-01-27 23:58:11 +0000315 OutStreamer.EmitLabel(CurrentFnSym);
316}
317
Jim Grosbach2317e402010-09-30 01:57:53 +0000318/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000319/// method to print assembly for each instruction.
320///
321bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000322 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000323 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000324
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000325 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000326}
327
Evan Cheng055b0312009-06-29 07:51:04 +0000328void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000329 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000330 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000331 unsigned TF = MO.getTargetFlags();
332
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000333 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000334 default:
335 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000336 case MachineOperand::MO_Register: {
337 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000338 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000339 assert(!MO.getSubReg() && "Subregs should be eliminated!");
340 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000341 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000342 }
Evan Chenga8e29892007-01-19 07:51:42 +0000343 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000344 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000345 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000346 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000347 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000348 O << ":lower16:";
349 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000350 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000351 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000352 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000353 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000354 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000355 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000356 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000357 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000358 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000359 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000360 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
361 (TF & ARMII::MO_LO16))
362 O << ":lower16:";
363 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
364 (TF & ARMII::MO_HI16))
365 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000366 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000367
Chris Lattner0c08d092010-04-03 22:28:33 +0000368 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000369 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000370 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000371 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000372 }
Evan Chenga8e29892007-01-19 07:51:42 +0000373 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000374 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000375 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000376 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000377 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000378 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000379 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000380 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000381 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000382 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000383 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000384 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000385 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000386}
387
Evan Cheng055b0312009-06-29 07:51:04 +0000388//===--------------------------------------------------------------------===//
389
Chris Lattner0890cf12010-01-25 19:51:38 +0000390MCSymbol *ARMAsmPrinter::
391GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
392 const MachineBasicBlock *MBB) const {
393 SmallString<60> Name;
394 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000395 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000396 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000397 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000398}
399
400MCSymbol *ARMAsmPrinter::
401GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
402 SmallString<60> Name;
403 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000404 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000405 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000406}
407
Jim Grosbach433a5782010-09-24 20:47:58 +0000408
409MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
410 SmallString<60> Name;
411 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
412 << getFunctionNumber();
413 return OutContext.GetOrCreateSymbol(Name.str());
414}
415
Evan Cheng055b0312009-06-29 07:51:04 +0000416bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000417 unsigned AsmVariant, const char *ExtraCode,
418 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000419 // Does this asm operand have a single letter operand modifier?
420 if (ExtraCode && ExtraCode[0]) {
421 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000422
Evan Chenga8e29892007-01-19 07:51:42 +0000423 switch (ExtraCode[0]) {
424 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000425 case 'a': // Print as a memory address.
426 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000427 O << "["
428 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
429 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000430 return false;
431 }
432 // Fallthrough
433 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000434 if (!MI->getOperand(OpNum).isImm())
435 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000436 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000437 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000438 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000439 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000440 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000441 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000442 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +0000443 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +0000444 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +0000445 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +0000446 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000447 }
Evan Chenga8e29892007-01-19 07:51:42 +0000448 }
Jim Grosbache9952212009-09-04 01:38:51 +0000449
Chris Lattner35c33bd2010-04-04 04:47:45 +0000450 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000451 return false;
452}
453
Bob Wilson224c2442009-05-19 05:53:42 +0000454bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000455 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000456 const char *ExtraCode,
457 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +0000458 if (ExtraCode && ExtraCode[0])
459 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +0000460
461 const MachineOperand &MO = MI->getOperand(OpNum);
462 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000463 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000464 return false;
465}
466
Bob Wilson812209a2009-09-30 22:06:26 +0000467void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000468 if (Subtarget->isTargetDarwin()) {
469 Reloc::Model RelocM = TM.getRelocationModel();
470 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
471 // Declare all the text sections up front (before the DWARF sections
472 // emitted by AsmPrinter::doInitialization) so the assembler will keep
473 // them together at the beginning of the object file. This helps
474 // avoid out-of-range branches that are due a fundamental limitation of
475 // the way symbol offsets are encoded with the current Darwin ARM
476 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000477 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000478 static_cast<const TargetLoweringObjectFileMachO &>(
479 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000480 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
481 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
482 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
483 if (RelocM == Reloc::DynamicNoPIC) {
484 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000485 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
486 MCSectionMachO::S_SYMBOL_STUBS,
487 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000488 OutStreamer.SwitchSection(sect);
489 } else {
490 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000491 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
492 MCSectionMachO::S_SYMBOL_STUBS,
493 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000494 OutStreamer.SwitchSection(sect);
495 }
Bob Wilson63db5942010-07-30 19:55:47 +0000496 const MCSection *StaticInitSect =
497 OutContext.getMachOSection("__TEXT", "__StaticInit",
498 MCSectionMachO::S_REGULAR |
499 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
500 SectionKind::getText());
501 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000502 }
503 }
504
Jim Grosbache5165492009-11-09 00:11:35 +0000505 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000506 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000507
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000508 // Emit ARM Build Attributes
509 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000510
Jason W Kimdef9ac42010-10-06 22:36:46 +0000511 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000512 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000513}
514
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000515
Chris Lattner4a071d62009-10-19 17:59:19 +0000516void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000517 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000518 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000519 const TargetLoweringObjectFileMachO &TLOFMacho =
520 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000521 MachineModuleInfoMachO &MMIMacho =
522 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000523
Evan Chenga8e29892007-01-19 07:51:42 +0000524 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000525 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000526
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000527 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000528 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000529 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000530 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000531 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000532 // L_foo$stub:
533 OutStreamer.EmitLabel(Stubs[i].first);
534 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000535 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
536 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000537
Bill Wendling52a50e52010-03-11 01:18:13 +0000538 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000539 // External to current translation unit.
540 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
541 else
542 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000543 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000544 // When we place the LSDA into the TEXT section, the type info
545 // pointers need to be indirect and pc-rel. We accomplish this by
546 // using NLPs; however, sometimes the types are local to the file.
547 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000548 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
549 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000550 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000551 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000552
553 Stubs.clear();
554 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000555 }
556
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000557 Stubs = MMIMacho.GetHiddenGVStubList();
558 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000559 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000560 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000561 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
562 // L_foo$stub:
563 OutStreamer.EmitLabel(Stubs[i].first);
564 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000565 OutStreamer.EmitValue(MCSymbolRefExpr::
566 Create(Stubs[i].second.getPointer(),
567 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000568 4/*size*/, 0/*addrspace*/);
569 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000570
571 Stubs.clear();
572 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000573 }
574
Evan Chenga8e29892007-01-19 07:51:42 +0000575 // Funny Darwin hack: This flag tells the linker that no global symbols
576 // contain code that falls through to other global symbols (e.g. the obvious
577 // implementation of multiple entry points). If this doesn't occur, the
578 // linker can safely perform dead code stripping. Since LLVM never
579 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000580 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000581 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000582}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000583
Chris Lattner97f06932009-10-19 20:20:46 +0000584//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000585// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
586// FIXME:
587// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000588// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000589// Instead of subclassing the MCELFStreamer, we do the work here.
590
591void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000592
Jason W Kim17b443d2010-10-11 23:01:44 +0000593 emitARMAttributeSection();
594
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000595 AttributeEmitter *AttrEmitter;
596 if (OutStreamer.hasRawTextSupport())
597 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
598 else {
599 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
600 AttrEmitter = new ObjectAttributeEmitter(O);
601 }
602
603 AttrEmitter->MaybeSwitchVendor("aeabi");
604
Jason W Kimdef9ac42010-10-06 22:36:46 +0000605 std::string CPUString = Subtarget->getCPUString();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000606 if (OutStreamer.hasRawTextSupport()) {
607 if (CPUString != "generic")
608 OutStreamer.EmitRawText(StringRef("\t.cpu ") + CPUString);
609 } else {
610 assert(CPUString == "generic" && "Unsupported .cpu attribute for ELF/.o");
611 // FIXME: Why these defaults?
612 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
613 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use, 1);
614 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 1);
615 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000616
617 // FIXME: Emit FPU type
618 if (Subtarget->hasVFP2())
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000619 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 2);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000620
621 // Signal various FP modes.
622 if (!UnsafeFPMath) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000623 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal, 1);
624 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000625 }
626
627 if (NoInfsFPMath && NoNaNsFPMath)
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000628 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000629 else
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000630 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 3);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000631
632 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000633 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
634 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000635
636 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
637 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000638 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
639 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000640 }
641 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000642
643 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
644
645 AttrEmitter->Finish();
646 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000647}
648
Jason W Kim17b443d2010-10-11 23:01:44 +0000649void ARMAsmPrinter::emitARMAttributeSection() {
650 // <format-version>
651 // [ <section-length> "vendor-name"
652 // [ <file-tag> <size> <attribute>*
653 // | <section-tag> <size> <section-number>* 0 <attribute>*
654 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
655 // ]+
656 // ]*
657
658 if (OutStreamer.hasRawTextSupport())
659 return;
660
661 const ARMElfTargetObjectFile &TLOFELF =
662 static_cast<const ARMElfTargetObjectFile &>
663 (getObjFileLowering());
664
665 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000666
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000667 // Format version
668 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000669}
670
Jason W Kimdef9ac42010-10-06 22:36:46 +0000671//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000672
Jim Grosbach988ce092010-09-18 00:05:05 +0000673static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
674 unsigned LabelId, MCContext &Ctx) {
675
676 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
677 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
678 return Label;
679}
680
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000681void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
682 unsigned Opcode = MI->getOpcode();
683 int OpNum = 1;
684 if (Opcode == ARM::BR_JTadd)
685 OpNum = 2;
686 else if (Opcode == ARM::BR_JTm)
687 OpNum = 3;
688
689 const MachineOperand &MO1 = MI->getOperand(OpNum);
690 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
691 unsigned JTI = MO1.getIndex();
692
693 // Emit a label for the jump table.
694 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
695 OutStreamer.EmitLabel(JTISymbol);
696
697 // Emit each entry of the table.
698 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
699 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
700 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
701
702 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
703 MachineBasicBlock *MBB = JTBBs[i];
704 // Construct an MCExpr for the entry. We want a value of the form:
705 // (BasicBlockAddr - TableBeginAddr)
706 //
707 // For example, a table with entries jumping to basic blocks BB0 and BB1
708 // would look like:
709 // LJTI_0_0:
710 // .word (LBB0 - LJTI_0_0)
711 // .word (LBB1 - LJTI_0_0)
712 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
713
714 if (TM.getRelocationModel() == Reloc::PIC_)
715 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
716 OutContext),
717 OutContext);
718 OutStreamer.EmitValue(Expr, 4);
719 }
720}
721
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000722void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
723 unsigned Opcode = MI->getOpcode();
724 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
725 const MachineOperand &MO1 = MI->getOperand(OpNum);
726 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
727 unsigned JTI = MO1.getIndex();
728
729 // Emit a label for the jump table.
730 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
731 OutStreamer.EmitLabel(JTISymbol);
732
733 // Emit each entry of the table.
734 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
735 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
736 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000737 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000738 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000739 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000740 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000741 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000742
743 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
744 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000745 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
746 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000747 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000748 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000749 MCInst BrInst;
750 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000751 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000752 OutStreamer.EmitInstruction(BrInst);
753 continue;
754 }
755 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000756 // MCExpr for the entry. We want a value of the form:
757 // (BasicBlockAddr - TableBeginAddr) / 2
758 //
759 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
760 // would look like:
761 // LJTI_0_0:
762 // .byte (LBB0 - LJTI_0_0) / 2
763 // .byte (LBB1 - LJTI_0_0) / 2
764 const MCExpr *Expr =
765 MCBinaryExpr::CreateSub(MBBSymbolExpr,
766 MCSymbolRefExpr::Create(JTISymbol, OutContext),
767 OutContext);
768 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
769 OutContext);
770 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000771 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000772
773 // Make sure the instruction that follows TBB is 2-byte aligned.
774 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
775 if (MI->getOpcode() == ARM::t2TBB)
776 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000777}
778
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000779void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
780 raw_ostream &OS) {
781 unsigned NOps = MI->getNumOperands();
782 assert(NOps==4);
783 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
784 // cast away const; DIetc do not take const operands for some reason.
785 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
786 OS << V.getName();
787 OS << " <- ";
788 // Frame address. Currently handles register +- offset only.
789 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
790 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
791 OS << ']';
792 OS << "+";
793 printOperand(MI, NOps-2, OS);
794}
795
Jim Grosbachb454cda2010-09-29 15:23:40 +0000796void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000797 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000798 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000799 case ARM::t2MOVi32imm:
800 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000801 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000802 case ARM::DBG_VALUE: {
803 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
804 SmallString<128> TmpStr;
805 raw_svector_ostream OS(TmpStr);
806 PrintDebugValueComment(MI, OS);
807 OutStreamer.EmitRawText(StringRef(OS.str()));
808 }
809 return;
810 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000811 case ARM::tPICADD: {
812 // This is a pseudo op for a label + instruction sequence, which looks like:
813 // LPC0:
814 // add r0, pc
815 // This adds the address of LPC0 to r0.
816
817 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000818 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
819 getFunctionNumber(), MI->getOperand(2).getImm(),
820 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000821
822 // Form and emit the add.
823 MCInst AddInst;
824 AddInst.setOpcode(ARM::tADDhirr);
825 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
826 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
827 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
828 // Add predicate operands.
829 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
830 AddInst.addOperand(MCOperand::CreateReg(0));
831 OutStreamer.EmitInstruction(AddInst);
832 return;
833 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000834 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +0000835 // This is a pseudo op for a label + instruction sequence, which looks like:
836 // LPC0:
837 // add r0, pc, r0
838 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000839
Chris Lattner4d152222009-10-19 22:23:04 +0000840 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000841 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
842 getFunctionNumber(), MI->getOperand(2).getImm(),
843 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000844
Jim Grosbachf3f09522010-09-14 21:05:34 +0000845 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000846 MCInst AddInst;
847 AddInst.setOpcode(ARM::ADDrr);
848 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
849 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
850 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000851 // Add predicate operands.
852 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
853 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
854 // Add 's' bit operand (always reg0 for this)
855 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000856 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000857 return;
858 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000859 case ARM::PICSTR:
860 case ARM::PICSTRB:
861 case ARM::PICSTRH:
862 case ARM::PICLDR:
863 case ARM::PICLDRB:
864 case ARM::PICLDRH:
865 case ARM::PICLDRSB:
866 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000867 // This is a pseudo op for a label + instruction sequence, which looks like:
868 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000869 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000870 // The LCP0 label is referenced by a constant pool entry in order to get
871 // a PC-relative address at the ldr instruction.
872
873 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000874 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
875 getFunctionNumber(), MI->getOperand(2).getImm(),
876 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000877
878 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000879 unsigned Opcode;
880 switch (MI->getOpcode()) {
881 default:
882 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000883 case ARM::PICSTR: Opcode = ARM::STRrs; break;
884 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000885 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000886 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +0000887 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000888 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
889 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
890 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
891 }
892 MCInst LdStInst;
893 LdStInst.setOpcode(Opcode);
894 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
895 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
896 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
897 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000898 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000899 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
900 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
901 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000902
903 return;
904 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000905 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +0000906 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
907 /// in the function. The first operand is the ID# for this instruction, the
908 /// second is the index into the MachineConstantPool that this is, the third
909 /// is the size in bytes of this constant pool entry.
910 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
911 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
912
913 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000914 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000915
916 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
917 if (MCPE.isMachineConstantPoolEntry())
918 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
919 else
920 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000921
Chris Lattnera70e6442009-10-19 22:33:05 +0000922 return;
923 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000924 case ARM::t2TBB:
925 case ARM::t2TBH:
926 case ARM::t2BR_JT: {
927 // Lower and emit the instruction itself, then the jump table following it.
928 MCInst TmpInst;
929 MCInstLowering.Lower(MI, TmpInst);
930 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000931 EmitJump2Table(MI);
932 return;
933 }
934 case ARM::tBR_JTr:
935 case ARM::BR_JTr:
936 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000937 case ARM::BR_JTadd: {
938 // Lower and emit the instruction itself, then the jump table following it.
939 MCInst TmpInst;
940 MCInstLowering.Lower(MI, TmpInst);
941 OutStreamer.EmitInstruction(TmpInst);
942 EmitJumpTable(MI);
943 return;
944 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000945 case ARM::TRAP: {
946 // Non-Darwin binutils don't yet support the "trap" mnemonic.
947 // FIXME: Remove this special case when they do.
948 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000949 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000950 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000951 OutStreamer.AddComment("trap");
952 OutStreamer.EmitIntValue(Val, 4);
953 return;
954 }
955 break;
956 }
957 case ARM::tTRAP: {
958 // Non-Darwin binutils don't yet support the "trap" mnemonic.
959 // FIXME: Remove this special case when they do.
960 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000961 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000962 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000963 OutStreamer.AddComment("trap");
964 OutStreamer.EmitIntValue(Val, 2);
965 return;
966 }
967 break;
968 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000969 case ARM::t2Int_eh_sjlj_setjmp:
970 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000971 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +0000972 // Two incoming args: GPR:$src, GPR:$val
973 // mov $val, pc
974 // adds $val, #7
975 // str $val, [$src, #4]
976 // movs r0, #0
977 // b 1f
978 // movs r0, #1
979 // 1:
980 unsigned SrcReg = MI->getOperand(0).getReg();
981 unsigned ValReg = MI->getOperand(1).getReg();
982 MCSymbol *Label = GetARMSJLJEHLabel();
983 {
984 MCInst TmpInst;
985 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
986 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
987 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
988 // 's' bit operand
989 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
990 OutStreamer.AddComment("eh_setjmp begin");
991 OutStreamer.EmitInstruction(TmpInst);
992 }
993 {
994 MCInst TmpInst;
995 TmpInst.setOpcode(ARM::tADDi3);
996 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
997 // 's' bit operand
998 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
999 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1000 TmpInst.addOperand(MCOperand::CreateImm(7));
1001 // Predicate.
1002 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1003 TmpInst.addOperand(MCOperand::CreateReg(0));
1004 OutStreamer.EmitInstruction(TmpInst);
1005 }
1006 {
1007 MCInst TmpInst;
1008 TmpInst.setOpcode(ARM::tSTR);
1009 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1010 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1011 // The offset immediate is #4. The operand value is scaled by 4 for the
1012 // tSTR instruction.
1013 TmpInst.addOperand(MCOperand::CreateImm(1));
1014 TmpInst.addOperand(MCOperand::CreateReg(0));
1015 // Predicate.
1016 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1017 TmpInst.addOperand(MCOperand::CreateReg(0));
1018 OutStreamer.EmitInstruction(TmpInst);
1019 }
1020 {
1021 MCInst TmpInst;
1022 TmpInst.setOpcode(ARM::tMOVi8);
1023 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1024 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1025 TmpInst.addOperand(MCOperand::CreateImm(0));
1026 // Predicate.
1027 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1028 TmpInst.addOperand(MCOperand::CreateReg(0));
1029 OutStreamer.EmitInstruction(TmpInst);
1030 }
1031 {
1032 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1033 MCInst TmpInst;
1034 TmpInst.setOpcode(ARM::tB);
1035 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1036 OutStreamer.EmitInstruction(TmpInst);
1037 }
1038 {
1039 MCInst TmpInst;
1040 TmpInst.setOpcode(ARM::tMOVi8);
1041 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1042 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1043 TmpInst.addOperand(MCOperand::CreateImm(1));
1044 // Predicate.
1045 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1046 TmpInst.addOperand(MCOperand::CreateReg(0));
1047 OutStreamer.AddComment("eh_setjmp end");
1048 OutStreamer.EmitInstruction(TmpInst);
1049 }
1050 OutStreamer.EmitLabel(Label);
1051 return;
1052 }
1053
Jim Grosbach45390082010-09-23 23:33:56 +00001054 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001055 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001056 // Two incoming args: GPR:$src, GPR:$val
1057 // add $val, pc, #8
1058 // str $val, [$src, #+4]
1059 // mov r0, #0
1060 // add pc, pc, #0
1061 // mov r0, #1
1062 unsigned SrcReg = MI->getOperand(0).getReg();
1063 unsigned ValReg = MI->getOperand(1).getReg();
1064
1065 {
1066 MCInst TmpInst;
1067 TmpInst.setOpcode(ARM::ADDri);
1068 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1069 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1070 TmpInst.addOperand(MCOperand::CreateImm(8));
1071 // Predicate.
1072 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1073 TmpInst.addOperand(MCOperand::CreateReg(0));
1074 // 's' bit operand (always reg0 for this).
1075 TmpInst.addOperand(MCOperand::CreateReg(0));
1076 OutStreamer.AddComment("eh_setjmp begin");
1077 OutStreamer.EmitInstruction(TmpInst);
1078 }
1079 {
1080 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001081 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001082 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1083 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001084 TmpInst.addOperand(MCOperand::CreateImm(4));
1085 // Predicate.
1086 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1087 TmpInst.addOperand(MCOperand::CreateReg(0));
1088 OutStreamer.EmitInstruction(TmpInst);
1089 }
1090 {
1091 MCInst TmpInst;
1092 TmpInst.setOpcode(ARM::MOVi);
1093 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1094 TmpInst.addOperand(MCOperand::CreateImm(0));
1095 // Predicate.
1096 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1097 TmpInst.addOperand(MCOperand::CreateReg(0));
1098 // 's' bit operand (always reg0 for this).
1099 TmpInst.addOperand(MCOperand::CreateReg(0));
1100 OutStreamer.EmitInstruction(TmpInst);
1101 }
1102 {
1103 MCInst TmpInst;
1104 TmpInst.setOpcode(ARM::ADDri);
1105 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1106 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1107 TmpInst.addOperand(MCOperand::CreateImm(0));
1108 // Predicate.
1109 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1110 TmpInst.addOperand(MCOperand::CreateReg(0));
1111 // 's' bit operand (always reg0 for this).
1112 TmpInst.addOperand(MCOperand::CreateReg(0));
1113 OutStreamer.EmitInstruction(TmpInst);
1114 }
1115 {
1116 MCInst TmpInst;
1117 TmpInst.setOpcode(ARM::MOVi);
1118 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1119 TmpInst.addOperand(MCOperand::CreateImm(1));
1120 // Predicate.
1121 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1122 TmpInst.addOperand(MCOperand::CreateReg(0));
1123 // 's' bit operand (always reg0 for this).
1124 TmpInst.addOperand(MCOperand::CreateReg(0));
1125 OutStreamer.AddComment("eh_setjmp end");
1126 OutStreamer.EmitInstruction(TmpInst);
1127 }
1128 return;
1129 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001130 case ARM::Int_eh_sjlj_longjmp: {
1131 // ldr sp, [$src, #8]
1132 // ldr $scratch, [$src, #4]
1133 // ldr r7, [$src]
1134 // bx $scratch
1135 unsigned SrcReg = MI->getOperand(0).getReg();
1136 unsigned ScratchReg = MI->getOperand(1).getReg();
1137 {
1138 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001139 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001140 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1141 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001142 TmpInst.addOperand(MCOperand::CreateImm(8));
1143 // Predicate.
1144 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1145 TmpInst.addOperand(MCOperand::CreateReg(0));
1146 OutStreamer.EmitInstruction(TmpInst);
1147 }
1148 {
1149 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001150 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001151 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1152 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001153 TmpInst.addOperand(MCOperand::CreateImm(4));
1154 // Predicate.
1155 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1156 TmpInst.addOperand(MCOperand::CreateReg(0));
1157 OutStreamer.EmitInstruction(TmpInst);
1158 }
1159 {
1160 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001161 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001162 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1163 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001164 TmpInst.addOperand(MCOperand::CreateImm(0));
1165 // Predicate.
1166 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1167 TmpInst.addOperand(MCOperand::CreateReg(0));
1168 OutStreamer.EmitInstruction(TmpInst);
1169 }
1170 {
1171 MCInst TmpInst;
1172 TmpInst.setOpcode(ARM::BRIND);
1173 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1174 // Predicate.
1175 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1176 TmpInst.addOperand(MCOperand::CreateReg(0));
1177 OutStreamer.EmitInstruction(TmpInst);
1178 }
1179 return;
1180 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001181 case ARM::tInt_eh_sjlj_longjmp: {
1182 // ldr $scratch, [$src, #8]
1183 // mov sp, $scratch
1184 // ldr $scratch, [$src, #4]
1185 // ldr r7, [$src]
1186 // bx $scratch
1187 unsigned SrcReg = MI->getOperand(0).getReg();
1188 unsigned ScratchReg = MI->getOperand(1).getReg();
1189 {
1190 MCInst TmpInst;
1191 TmpInst.setOpcode(ARM::tLDR);
1192 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1193 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1194 // The offset immediate is #8. The operand value is scaled by 4 for the
1195 // tSTR instruction.
1196 TmpInst.addOperand(MCOperand::CreateImm(2));
1197 TmpInst.addOperand(MCOperand::CreateReg(0));
1198 // Predicate.
1199 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1200 TmpInst.addOperand(MCOperand::CreateReg(0));
1201 OutStreamer.EmitInstruction(TmpInst);
1202 }
1203 {
1204 MCInst TmpInst;
1205 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1206 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1207 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1208 // Predicate.
1209 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1210 TmpInst.addOperand(MCOperand::CreateReg(0));
1211 OutStreamer.EmitInstruction(TmpInst);
1212 }
1213 {
1214 MCInst TmpInst;
1215 TmpInst.setOpcode(ARM::tLDR);
1216 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1217 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1218 TmpInst.addOperand(MCOperand::CreateImm(1));
1219 TmpInst.addOperand(MCOperand::CreateReg(0));
1220 // Predicate.
1221 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1222 TmpInst.addOperand(MCOperand::CreateReg(0));
1223 OutStreamer.EmitInstruction(TmpInst);
1224 }
1225 {
1226 MCInst TmpInst;
1227 TmpInst.setOpcode(ARM::tLDR);
1228 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1229 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1230 TmpInst.addOperand(MCOperand::CreateImm(0));
1231 TmpInst.addOperand(MCOperand::CreateReg(0));
1232 // Predicate.
1233 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1234 TmpInst.addOperand(MCOperand::CreateReg(0));
1235 OutStreamer.EmitInstruction(TmpInst);
1236 }
1237 {
1238 MCInst TmpInst;
1239 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1240 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1241 // Predicate.
1242 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1243 TmpInst.addOperand(MCOperand::CreateReg(0));
1244 OutStreamer.EmitInstruction(TmpInst);
1245 }
1246 return;
1247 }
Chris Lattner97f06932009-10-19 20:20:46 +00001248 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001249
Chris Lattner97f06932009-10-19 20:20:46 +00001250 MCInst TmpInst;
1251 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001252 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001253}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001254
1255//===----------------------------------------------------------------------===//
1256// Target Registry Stuff
1257//===----------------------------------------------------------------------===//
1258
1259static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1260 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001261 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001262 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001263 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001264 return 0;
1265}
1266
1267// Force static initialization.
1268extern "C" void LLVMInitializeARMAsmPrinter() {
1269 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1270 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1271
1272 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1273 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1274}
1275