blob: 73d3e1caf2ed5edd32cd978b8e8df80648ea76c4 [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.
Jim Grosbach5df08d82010-11-09 18:45:04 +0000230 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000231 };
232} // end of anonymous namespace
233
Chris Lattner953ebb72010-01-27 23:58:11 +0000234void ARMAsmPrinter::EmitFunctionEntryLabel() {
235 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000236 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
237 OutStreamer.EmitThumbFunc(Subtarget->isTargetDarwin()? CurrentFnSym : 0);
Chris Lattner953ebb72010-01-27 23:58:11 +0000238 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000239
Chris Lattner953ebb72010-01-27 23:58:11 +0000240 OutStreamer.EmitLabel(CurrentFnSym);
241}
242
Jim Grosbach2317e402010-09-30 01:57:53 +0000243/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000244/// method to print assembly for each instruction.
245///
246bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000247 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000248 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000249
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000250 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000251}
252
Evan Cheng055b0312009-06-29 07:51:04 +0000253void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000254 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000255 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000256 unsigned TF = MO.getTargetFlags();
257
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000258 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000259 default:
260 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000261 case MachineOperand::MO_Register: {
262 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000263 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000264 assert(!MO.getSubReg() && "Subregs should be eliminated!");
265 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000266 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000267 }
Evan Chenga8e29892007-01-19 07:51:42 +0000268 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000269 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000270 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000271 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000272 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000273 O << ":lower16:";
274 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000275 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000276 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000277 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000278 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000279 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000280 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000281 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000282 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000283 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000284 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000285 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
286 (TF & ARMII::MO_LO16))
287 O << ":lower16:";
288 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
289 (TF & ARMII::MO_HI16))
290 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000291 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000292
Chris Lattner0c08d092010-04-03 22:28:33 +0000293 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000294 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000295 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000296 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000297 }
Evan Chenga8e29892007-01-19 07:51:42 +0000298 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000299 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000300 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000301 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000302 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000303 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000304 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000305 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000306 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000307 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000308 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000309 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000310 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000311}
312
Evan Cheng055b0312009-06-29 07:51:04 +0000313//===--------------------------------------------------------------------===//
314
Chris Lattner0890cf12010-01-25 19:51:38 +0000315MCSymbol *ARMAsmPrinter::
316GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
317 const MachineBasicBlock *MBB) const {
318 SmallString<60> Name;
319 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000320 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000321 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000322 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000323}
324
325MCSymbol *ARMAsmPrinter::
326GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
327 SmallString<60> Name;
328 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000329 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000330 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000331}
332
Jim Grosbach433a5782010-09-24 20:47:58 +0000333
334MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
335 SmallString<60> Name;
336 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
337 << getFunctionNumber();
338 return OutContext.GetOrCreateSymbol(Name.str());
339}
340
Evan Cheng055b0312009-06-29 07:51:04 +0000341bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000342 unsigned AsmVariant, const char *ExtraCode,
343 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000344 // Does this asm operand have a single letter operand modifier?
345 if (ExtraCode && ExtraCode[0]) {
346 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000347
Evan Chenga8e29892007-01-19 07:51:42 +0000348 switch (ExtraCode[0]) {
349 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000350 case 'a': // Print as a memory address.
351 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000352 O << "["
353 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
354 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000355 return false;
356 }
357 // Fallthrough
358 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000359 if (!MI->getOperand(OpNum).isImm())
360 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000361 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000362 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000363 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000364 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000365 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000366 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000367 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +0000368 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +0000369 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +0000370 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +0000371 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000372 }
Evan Chenga8e29892007-01-19 07:51:42 +0000373 }
Jim Grosbache9952212009-09-04 01:38:51 +0000374
Chris Lattner35c33bd2010-04-04 04:47:45 +0000375 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000376 return false;
377}
378
Bob Wilson224c2442009-05-19 05:53:42 +0000379bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000380 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000381 const char *ExtraCode,
382 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +0000383 if (ExtraCode && ExtraCode[0])
384 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +0000385
386 const MachineOperand &MO = MI->getOperand(OpNum);
387 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000388 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000389 return false;
390}
391
Bob Wilson812209a2009-09-30 22:06:26 +0000392void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000393 if (Subtarget->isTargetDarwin()) {
394 Reloc::Model RelocM = TM.getRelocationModel();
395 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
396 // Declare all the text sections up front (before the DWARF sections
397 // emitted by AsmPrinter::doInitialization) so the assembler will keep
398 // them together at the beginning of the object file. This helps
399 // avoid out-of-range branches that are due a fundamental limitation of
400 // the way symbol offsets are encoded with the current Darwin ARM
401 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000402 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000403 static_cast<const TargetLoweringObjectFileMachO &>(
404 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000405 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
406 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
407 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
408 if (RelocM == Reloc::DynamicNoPIC) {
409 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000410 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
411 MCSectionMachO::S_SYMBOL_STUBS,
412 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000413 OutStreamer.SwitchSection(sect);
414 } else {
415 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000416 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
417 MCSectionMachO::S_SYMBOL_STUBS,
418 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000419 OutStreamer.SwitchSection(sect);
420 }
Bob Wilson63db5942010-07-30 19:55:47 +0000421 const MCSection *StaticInitSect =
422 OutContext.getMachOSection("__TEXT", "__StaticInit",
423 MCSectionMachO::S_REGULAR |
424 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
425 SectionKind::getText());
426 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000427 }
428 }
429
Jim Grosbache5165492009-11-09 00:11:35 +0000430 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000431 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000432
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000433 // Emit ARM Build Attributes
434 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000435
Jason W Kimdef9ac42010-10-06 22:36:46 +0000436 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000437 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000438}
439
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000440
Chris Lattner4a071d62009-10-19 17:59:19 +0000441void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000442 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000443 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000444 const TargetLoweringObjectFileMachO &TLOFMacho =
445 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000446 MachineModuleInfoMachO &MMIMacho =
447 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000448
Evan Chenga8e29892007-01-19 07:51:42 +0000449 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000450 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000451
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000452 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000453 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000454 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000455 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000456 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000457 // L_foo$stub:
458 OutStreamer.EmitLabel(Stubs[i].first);
459 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000460 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
461 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000462
Bill Wendling52a50e52010-03-11 01:18:13 +0000463 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000464 // External to current translation unit.
465 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
466 else
467 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000468 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000469 // When we place the LSDA into the TEXT section, the type info
470 // pointers need to be indirect and pc-rel. We accomplish this by
471 // using NLPs; however, sometimes the types are local to the file.
472 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000473 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
474 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000475 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000476 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000477
478 Stubs.clear();
479 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000480 }
481
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000482 Stubs = MMIMacho.GetHiddenGVStubList();
483 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000484 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000485 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000486 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
487 // L_foo$stub:
488 OutStreamer.EmitLabel(Stubs[i].first);
489 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000490 OutStreamer.EmitValue(MCSymbolRefExpr::
491 Create(Stubs[i].second.getPointer(),
492 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000493 4/*size*/, 0/*addrspace*/);
494 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000495
496 Stubs.clear();
497 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000498 }
499
Evan Chenga8e29892007-01-19 07:51:42 +0000500 // Funny Darwin hack: This flag tells the linker that no global symbols
501 // contain code that falls through to other global symbols (e.g. the obvious
502 // implementation of multiple entry points). If this doesn't occur, the
503 // linker can safely perform dead code stripping. Since LLVM never
504 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000505 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000506 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000507}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000508
Chris Lattner97f06932009-10-19 20:20:46 +0000509//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000510// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
511// FIXME:
512// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000513// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000514// Instead of subclassing the MCELFStreamer, we do the work here.
515
516void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000517
Jason W Kim17b443d2010-10-11 23:01:44 +0000518 emitARMAttributeSection();
519
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000520 AttributeEmitter *AttrEmitter;
521 if (OutStreamer.hasRawTextSupport())
522 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
523 else {
524 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
525 AttrEmitter = new ObjectAttributeEmitter(O);
526 }
527
528 AttrEmitter->MaybeSwitchVendor("aeabi");
529
Jason W Kimdef9ac42010-10-06 22:36:46 +0000530 std::string CPUString = Subtarget->getCPUString();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000531 if (OutStreamer.hasRawTextSupport()) {
532 if (CPUString != "generic")
533 OutStreamer.EmitRawText(StringRef("\t.cpu ") + CPUString);
534 } else {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000535 assert(CPUString == "generic" && "Unsupported .cpu attribute for ELF/.o");
536 // FIXME: Why these defaults?
537 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
538 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use, 1);
539 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000540 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000541
542 // FIXME: Emit FPU type
543 if (Subtarget->hasVFP2())
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000544 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch, 2);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000545
546 // Signal various FP modes.
547 if (!UnsafeFPMath) {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000548 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal, 1);
549 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000550 }
551
552 if (NoInfsFPMath && NoNaNsFPMath)
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000553 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000554 else
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000555 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model, 3);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000556
557 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000558 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
559 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000560
561 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
562 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000563 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
564 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000565 }
566 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000567
568 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
569
570 AttrEmitter->Finish();
571 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000572}
573
Jason W Kim17b443d2010-10-11 23:01:44 +0000574void ARMAsmPrinter::emitARMAttributeSection() {
575 // <format-version>
576 // [ <section-length> "vendor-name"
577 // [ <file-tag> <size> <attribute>*
578 // | <section-tag> <size> <section-number>* 0 <attribute>*
579 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
580 // ]+
581 // ]*
582
583 if (OutStreamer.hasRawTextSupport())
584 return;
585
586 const ARMElfTargetObjectFile &TLOFELF =
587 static_cast<const ARMElfTargetObjectFile &>
588 (getObjFileLowering());
589
590 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000591
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000592 // Format version
593 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000594}
595
Jason W Kimdef9ac42010-10-06 22:36:46 +0000596//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000597
Jim Grosbach988ce092010-09-18 00:05:05 +0000598static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
599 unsigned LabelId, MCContext &Ctx) {
600
601 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
602 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
603 return Label;
604}
605
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000606static MCSymbolRefExpr::VariantKind
607getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
608 switch (Modifier) {
609 default: llvm_unreachable("Unknown modifier!");
610 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
611 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
612 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
613 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
614 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
615 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
616 }
617 return MCSymbolRefExpr::VK_None;
618}
619
Jim Grosbach5df08d82010-11-09 18:45:04 +0000620void ARMAsmPrinter::
621EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
622 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
623
624 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000625
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000626 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000627 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000628 SmallString<128> Str;
629 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000630 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000631 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000632 } else if (ACPV->isBlockAddress()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000633 MCSym = GetBlockAddressSymbol(ACPV->getBlockAddress());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000634 } else if (ACPV->isGlobalValue()) {
635 const GlobalValue *GV = ACPV->getGV();
636 bool isIndirect = Subtarget->isTargetDarwin() &&
637 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
638 if (!isIndirect)
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000639 MCSym = Mang->getSymbol(GV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000640 else {
641 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000642 MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Jim Grosbach5df08d82010-11-09 18:45:04 +0000643
644 MachineModuleInfoMachO &MMIMachO =
645 MMI->getObjFileInfo<MachineModuleInfoMachO>();
646 MachineModuleInfoImpl::StubValueTy &StubSym =
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000647 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
648 MMIMachO.getGVStubEntry(MCSym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000649 if (StubSym.getPointer() == 0)
650 StubSym = MachineModuleInfoImpl::
651 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
652 }
653 } else {
654 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000655 MCSym = GetExternalSymbolSymbol(ACPV->getSymbol());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000656 }
657
658 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000659 const MCExpr *Expr =
660 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
661 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000662
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000663 if (ACPV->getPCAdjustment()) {
664 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
665 getFunctionNumber(),
666 ACPV->getLabelId(),
667 OutContext);
668 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
669 PCRelExpr =
670 MCBinaryExpr::CreateAdd(PCRelExpr,
671 MCConstantExpr::Create(ACPV->getPCAdjustment(),
672 OutContext),
673 OutContext);
674 if (ACPV->mustAddCurrentAddress()) {
675 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
676 // label, so just emit a local label end reference that instead.
677 MCSymbol *DotSym = OutContext.CreateTempSymbol();
678 OutStreamer.EmitLabel(DotSym);
679 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
680 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000681 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000682 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000683 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000684 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000685}
686
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000687void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
688 unsigned Opcode = MI->getOpcode();
689 int OpNum = 1;
690 if (Opcode == ARM::BR_JTadd)
691 OpNum = 2;
692 else if (Opcode == ARM::BR_JTm)
693 OpNum = 3;
694
695 const MachineOperand &MO1 = MI->getOperand(OpNum);
696 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
697 unsigned JTI = MO1.getIndex();
698
699 // Emit a label for the jump table.
700 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
701 OutStreamer.EmitLabel(JTISymbol);
702
703 // Emit each entry of the table.
704 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
705 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
706 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
707
708 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
709 MachineBasicBlock *MBB = JTBBs[i];
710 // Construct an MCExpr for the entry. We want a value of the form:
711 // (BasicBlockAddr - TableBeginAddr)
712 //
713 // For example, a table with entries jumping to basic blocks BB0 and BB1
714 // would look like:
715 // LJTI_0_0:
716 // .word (LBB0 - LJTI_0_0)
717 // .word (LBB1 - LJTI_0_0)
718 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
719
720 if (TM.getRelocationModel() == Reloc::PIC_)
721 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
722 OutContext),
723 OutContext);
724 OutStreamer.EmitValue(Expr, 4);
725 }
726}
727
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000728void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
729 unsigned Opcode = MI->getOpcode();
730 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
731 const MachineOperand &MO1 = MI->getOperand(OpNum);
732 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
733 unsigned JTI = MO1.getIndex();
734
735 // Emit a label for the jump table.
736 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
737 OutStreamer.EmitLabel(JTISymbol);
738
739 // Emit each entry of the table.
740 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
741 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
742 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000743 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000744 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000745 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000746 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000747 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000748
749 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
750 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000751 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
752 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000753 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000754 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000755 MCInst BrInst;
756 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000757 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000758 OutStreamer.EmitInstruction(BrInst);
759 continue;
760 }
761 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000762 // MCExpr for the entry. We want a value of the form:
763 // (BasicBlockAddr - TableBeginAddr) / 2
764 //
765 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
766 // would look like:
767 // LJTI_0_0:
768 // .byte (LBB0 - LJTI_0_0) / 2
769 // .byte (LBB1 - LJTI_0_0) / 2
770 const MCExpr *Expr =
771 MCBinaryExpr::CreateSub(MBBSymbolExpr,
772 MCSymbolRefExpr::Create(JTISymbol, OutContext),
773 OutContext);
774 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
775 OutContext);
776 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000777 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000778
779 // Make sure the instruction that follows TBB is 2-byte aligned.
780 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
781 if (MI->getOpcode() == ARM::t2TBB)
782 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000783}
784
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000785void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
786 raw_ostream &OS) {
787 unsigned NOps = MI->getNumOperands();
788 assert(NOps==4);
789 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
790 // cast away const; DIetc do not take const operands for some reason.
791 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
792 OS << V.getName();
793 OS << " <- ";
794 // Frame address. Currently handles register +- offset only.
795 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
796 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
797 OS << ']';
798 OS << "+";
799 printOperand(MI, NOps-2, OS);
800}
801
Jim Grosbachb454cda2010-09-29 15:23:40 +0000802void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000803 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000804 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000805 case ARM::t2MOVi32imm:
806 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000807 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000808 case ARM::DBG_VALUE: {
809 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
810 SmallString<128> TmpStr;
811 raw_svector_ostream OS(TmpStr);
812 PrintDebugValueComment(MI, OS);
813 OutStreamer.EmitRawText(StringRef(OS.str()));
814 }
815 return;
816 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000817 case ARM::tPICADD: {
818 // This is a pseudo op for a label + instruction sequence, which looks like:
819 // LPC0:
820 // add r0, pc
821 // This adds the address of LPC0 to r0.
822
823 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000824 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
825 getFunctionNumber(), MI->getOperand(2).getImm(),
826 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000827
828 // Form and emit the add.
829 MCInst AddInst;
830 AddInst.setOpcode(ARM::tADDhirr);
831 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
832 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
833 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
834 // Add predicate operands.
835 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
836 AddInst.addOperand(MCOperand::CreateReg(0));
837 OutStreamer.EmitInstruction(AddInst);
838 return;
839 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000840 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +0000841 // This is a pseudo op for a label + instruction sequence, which looks like:
842 // LPC0:
843 // add r0, pc, r0
844 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000845
Chris Lattner4d152222009-10-19 22:23:04 +0000846 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000847 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
848 getFunctionNumber(), MI->getOperand(2).getImm(),
849 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000850
Jim Grosbachf3f09522010-09-14 21:05:34 +0000851 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000852 MCInst AddInst;
853 AddInst.setOpcode(ARM::ADDrr);
854 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
855 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
856 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000857 // Add predicate operands.
858 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
859 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
860 // Add 's' bit operand (always reg0 for this)
861 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000862 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000863 return;
864 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000865 case ARM::PICSTR:
866 case ARM::PICSTRB:
867 case ARM::PICSTRH:
868 case ARM::PICLDR:
869 case ARM::PICLDRB:
870 case ARM::PICLDRH:
871 case ARM::PICLDRSB:
872 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000873 // This is a pseudo op for a label + instruction sequence, which looks like:
874 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000875 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000876 // The LCP0 label is referenced by a constant pool entry in order to get
877 // a PC-relative address at the ldr instruction.
878
879 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000880 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
881 getFunctionNumber(), MI->getOperand(2).getImm(),
882 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000883
884 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000885 unsigned Opcode;
886 switch (MI->getOpcode()) {
887 default:
888 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000889 case ARM::PICSTR: Opcode = ARM::STRrs; break;
890 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000891 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000892 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +0000893 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000894 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
895 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
896 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
897 }
898 MCInst LdStInst;
899 LdStInst.setOpcode(Opcode);
900 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
901 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
902 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
903 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000904 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000905 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
906 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
907 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000908
909 return;
910 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000911 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +0000912 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
913 /// in the function. The first operand is the ID# for this instruction, the
914 /// second is the index into the MachineConstantPool that this is, the third
915 /// is the size in bytes of this constant pool entry.
916 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
917 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
918
919 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000920 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000921
922 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
923 if (MCPE.isMachineConstantPoolEntry())
924 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
925 else
926 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000927
Chris Lattnera70e6442009-10-19 22:33:05 +0000928 return;
929 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000930 case ARM::t2TBB:
931 case ARM::t2TBH:
932 case ARM::t2BR_JT: {
933 // Lower and emit the instruction itself, then the jump table following it.
934 MCInst TmpInst;
935 MCInstLowering.Lower(MI, TmpInst);
936 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000937 EmitJump2Table(MI);
938 return;
939 }
940 case ARM::tBR_JTr:
941 case ARM::BR_JTr:
942 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000943 case ARM::BR_JTadd: {
944 // Lower and emit the instruction itself, then the jump table following it.
945 MCInst TmpInst;
946 MCInstLowering.Lower(MI, TmpInst);
947 OutStreamer.EmitInstruction(TmpInst);
948 EmitJumpTable(MI);
949 return;
950 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000951 case ARM::TRAP: {
952 // Non-Darwin binutils don't yet support the "trap" mnemonic.
953 // FIXME: Remove this special case when they do.
954 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000955 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000956 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000957 OutStreamer.AddComment("trap");
958 OutStreamer.EmitIntValue(Val, 4);
959 return;
960 }
961 break;
962 }
963 case ARM::tTRAP: {
964 // Non-Darwin binutils don't yet support the "trap" mnemonic.
965 // FIXME: Remove this special case when they do.
966 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000967 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000968 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000969 OutStreamer.AddComment("trap");
970 OutStreamer.EmitIntValue(Val, 2);
971 return;
972 }
973 break;
974 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000975 case ARM::t2Int_eh_sjlj_setjmp:
976 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000977 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +0000978 // Two incoming args: GPR:$src, GPR:$val
979 // mov $val, pc
980 // adds $val, #7
981 // str $val, [$src, #4]
982 // movs r0, #0
983 // b 1f
984 // movs r0, #1
985 // 1:
986 unsigned SrcReg = MI->getOperand(0).getReg();
987 unsigned ValReg = MI->getOperand(1).getReg();
988 MCSymbol *Label = GetARMSJLJEHLabel();
989 {
990 MCInst TmpInst;
991 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
992 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
993 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
994 // 's' bit operand
995 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
996 OutStreamer.AddComment("eh_setjmp begin");
997 OutStreamer.EmitInstruction(TmpInst);
998 }
999 {
1000 MCInst TmpInst;
1001 TmpInst.setOpcode(ARM::tADDi3);
1002 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1003 // 's' bit operand
1004 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1005 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1006 TmpInst.addOperand(MCOperand::CreateImm(7));
1007 // Predicate.
1008 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1009 TmpInst.addOperand(MCOperand::CreateReg(0));
1010 OutStreamer.EmitInstruction(TmpInst);
1011 }
1012 {
1013 MCInst TmpInst;
1014 TmpInst.setOpcode(ARM::tSTR);
1015 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1016 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1017 // The offset immediate is #4. The operand value is scaled by 4 for the
1018 // tSTR instruction.
1019 TmpInst.addOperand(MCOperand::CreateImm(1));
1020 TmpInst.addOperand(MCOperand::CreateReg(0));
1021 // Predicate.
1022 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1023 TmpInst.addOperand(MCOperand::CreateReg(0));
1024 OutStreamer.EmitInstruction(TmpInst);
1025 }
1026 {
1027 MCInst TmpInst;
1028 TmpInst.setOpcode(ARM::tMOVi8);
1029 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1030 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1031 TmpInst.addOperand(MCOperand::CreateImm(0));
1032 // Predicate.
1033 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1034 TmpInst.addOperand(MCOperand::CreateReg(0));
1035 OutStreamer.EmitInstruction(TmpInst);
1036 }
1037 {
1038 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1039 MCInst TmpInst;
1040 TmpInst.setOpcode(ARM::tB);
1041 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1042 OutStreamer.EmitInstruction(TmpInst);
1043 }
1044 {
1045 MCInst TmpInst;
1046 TmpInst.setOpcode(ARM::tMOVi8);
1047 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1048 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1049 TmpInst.addOperand(MCOperand::CreateImm(1));
1050 // Predicate.
1051 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1052 TmpInst.addOperand(MCOperand::CreateReg(0));
1053 OutStreamer.AddComment("eh_setjmp end");
1054 OutStreamer.EmitInstruction(TmpInst);
1055 }
1056 OutStreamer.EmitLabel(Label);
1057 return;
1058 }
1059
Jim Grosbach45390082010-09-23 23:33:56 +00001060 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001061 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001062 // Two incoming args: GPR:$src, GPR:$val
1063 // add $val, pc, #8
1064 // str $val, [$src, #+4]
1065 // mov r0, #0
1066 // add pc, pc, #0
1067 // mov r0, #1
1068 unsigned SrcReg = MI->getOperand(0).getReg();
1069 unsigned ValReg = MI->getOperand(1).getReg();
1070
1071 {
1072 MCInst TmpInst;
1073 TmpInst.setOpcode(ARM::ADDri);
1074 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1075 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1076 TmpInst.addOperand(MCOperand::CreateImm(8));
1077 // Predicate.
1078 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1079 TmpInst.addOperand(MCOperand::CreateReg(0));
1080 // 's' bit operand (always reg0 for this).
1081 TmpInst.addOperand(MCOperand::CreateReg(0));
1082 OutStreamer.AddComment("eh_setjmp begin");
1083 OutStreamer.EmitInstruction(TmpInst);
1084 }
1085 {
1086 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001087 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001088 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1089 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001090 TmpInst.addOperand(MCOperand::CreateImm(4));
1091 // Predicate.
1092 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1093 TmpInst.addOperand(MCOperand::CreateReg(0));
1094 OutStreamer.EmitInstruction(TmpInst);
1095 }
1096 {
1097 MCInst TmpInst;
1098 TmpInst.setOpcode(ARM::MOVi);
1099 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1100 TmpInst.addOperand(MCOperand::CreateImm(0));
1101 // Predicate.
1102 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1103 TmpInst.addOperand(MCOperand::CreateReg(0));
1104 // 's' bit operand (always reg0 for this).
1105 TmpInst.addOperand(MCOperand::CreateReg(0));
1106 OutStreamer.EmitInstruction(TmpInst);
1107 }
1108 {
1109 MCInst TmpInst;
1110 TmpInst.setOpcode(ARM::ADDri);
1111 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1112 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1113 TmpInst.addOperand(MCOperand::CreateImm(0));
1114 // Predicate.
1115 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1116 TmpInst.addOperand(MCOperand::CreateReg(0));
1117 // 's' bit operand (always reg0 for this).
1118 TmpInst.addOperand(MCOperand::CreateReg(0));
1119 OutStreamer.EmitInstruction(TmpInst);
1120 }
1121 {
1122 MCInst TmpInst;
1123 TmpInst.setOpcode(ARM::MOVi);
1124 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1125 TmpInst.addOperand(MCOperand::CreateImm(1));
1126 // Predicate.
1127 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1128 TmpInst.addOperand(MCOperand::CreateReg(0));
1129 // 's' bit operand (always reg0 for this).
1130 TmpInst.addOperand(MCOperand::CreateReg(0));
1131 OutStreamer.AddComment("eh_setjmp end");
1132 OutStreamer.EmitInstruction(TmpInst);
1133 }
1134 return;
1135 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001136 case ARM::Int_eh_sjlj_longjmp: {
1137 // ldr sp, [$src, #8]
1138 // ldr $scratch, [$src, #4]
1139 // ldr r7, [$src]
1140 // bx $scratch
1141 unsigned SrcReg = MI->getOperand(0).getReg();
1142 unsigned ScratchReg = MI->getOperand(1).getReg();
1143 {
1144 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001145 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001146 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1147 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001148 TmpInst.addOperand(MCOperand::CreateImm(8));
1149 // Predicate.
1150 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1151 TmpInst.addOperand(MCOperand::CreateReg(0));
1152 OutStreamer.EmitInstruction(TmpInst);
1153 }
1154 {
1155 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001156 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001157 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1158 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001159 TmpInst.addOperand(MCOperand::CreateImm(4));
1160 // Predicate.
1161 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1162 TmpInst.addOperand(MCOperand::CreateReg(0));
1163 OutStreamer.EmitInstruction(TmpInst);
1164 }
1165 {
1166 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001167 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001168 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1169 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001170 TmpInst.addOperand(MCOperand::CreateImm(0));
1171 // Predicate.
1172 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1173 TmpInst.addOperand(MCOperand::CreateReg(0));
1174 OutStreamer.EmitInstruction(TmpInst);
1175 }
1176 {
1177 MCInst TmpInst;
1178 TmpInst.setOpcode(ARM::BRIND);
1179 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1180 // Predicate.
1181 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1182 TmpInst.addOperand(MCOperand::CreateReg(0));
1183 OutStreamer.EmitInstruction(TmpInst);
1184 }
1185 return;
1186 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001187 case ARM::tInt_eh_sjlj_longjmp: {
1188 // ldr $scratch, [$src, #8]
1189 // mov sp, $scratch
1190 // ldr $scratch, [$src, #4]
1191 // ldr r7, [$src]
1192 // bx $scratch
1193 unsigned SrcReg = MI->getOperand(0).getReg();
1194 unsigned ScratchReg = MI->getOperand(1).getReg();
1195 {
1196 MCInst TmpInst;
1197 TmpInst.setOpcode(ARM::tLDR);
1198 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1199 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1200 // The offset immediate is #8. The operand value is scaled by 4 for the
1201 // tSTR instruction.
1202 TmpInst.addOperand(MCOperand::CreateImm(2));
1203 TmpInst.addOperand(MCOperand::CreateReg(0));
1204 // Predicate.
1205 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1206 TmpInst.addOperand(MCOperand::CreateReg(0));
1207 OutStreamer.EmitInstruction(TmpInst);
1208 }
1209 {
1210 MCInst TmpInst;
1211 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1212 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1213 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1214 // Predicate.
1215 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1216 TmpInst.addOperand(MCOperand::CreateReg(0));
1217 OutStreamer.EmitInstruction(TmpInst);
1218 }
1219 {
1220 MCInst TmpInst;
1221 TmpInst.setOpcode(ARM::tLDR);
1222 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1223 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1224 TmpInst.addOperand(MCOperand::CreateImm(1));
1225 TmpInst.addOperand(MCOperand::CreateReg(0));
1226 // Predicate.
1227 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1228 TmpInst.addOperand(MCOperand::CreateReg(0));
1229 OutStreamer.EmitInstruction(TmpInst);
1230 }
1231 {
1232 MCInst TmpInst;
1233 TmpInst.setOpcode(ARM::tLDR);
1234 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1235 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1236 TmpInst.addOperand(MCOperand::CreateImm(0));
1237 TmpInst.addOperand(MCOperand::CreateReg(0));
1238 // Predicate.
1239 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1240 TmpInst.addOperand(MCOperand::CreateReg(0));
1241 OutStreamer.EmitInstruction(TmpInst);
1242 }
1243 {
1244 MCInst TmpInst;
1245 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1246 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1247 // Predicate.
1248 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1249 TmpInst.addOperand(MCOperand::CreateReg(0));
1250 OutStreamer.EmitInstruction(TmpInst);
1251 }
1252 return;
1253 }
Chris Lattner97f06932009-10-19 20:20:46 +00001254 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001255
Chris Lattner97f06932009-10-19 20:20:46 +00001256 MCInst TmpInst;
1257 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001258 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001259}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001260
1261//===----------------------------------------------------------------------===//
1262// Target Registry Stuff
1263//===----------------------------------------------------------------------===//
1264
1265static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1266 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001267 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001268 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001269 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001270 return 0;
1271}
1272
1273// Force static initialization.
1274extern "C" void LLVMInitializeARMAsmPrinter() {
1275 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1276 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1277
1278 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1279 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1280}
1281