blob: ed7dd8c0de9275cba24b16ffd32636150a2597c9 [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 Grosbach5df08d82010-11-09 18:45:04 +0000606void ARMAsmPrinter::
607EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
608 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
609
610 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
611 SmallString<128> Str;
612 raw_svector_ostream OS(Str);
613
614 if (ACPV->isLSDA()) {
615 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
616 } else if (ACPV->isBlockAddress()) {
617 OS << *GetBlockAddressSymbol(ACPV->getBlockAddress());
618 } else if (ACPV->isGlobalValue()) {
619 const GlobalValue *GV = ACPV->getGV();
620 bool isIndirect = Subtarget->isTargetDarwin() &&
621 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
622 if (!isIndirect)
623 OS << *Mang->getSymbol(GV);
624 else {
625 // FIXME: Remove this when Darwin transition to @GOT like syntax.
626 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
627 OS << *Sym;
628
629 MachineModuleInfoMachO &MMIMachO =
630 MMI->getObjFileInfo<MachineModuleInfoMachO>();
631 MachineModuleInfoImpl::StubValueTy &StubSym =
632 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
633 MMIMachO.getGVStubEntry(Sym);
634 if (StubSym.getPointer() == 0)
635 StubSym = MachineModuleInfoImpl::
636 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
637 }
638 } else {
639 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
640 OS << *GetExternalSymbolSymbol(ACPV->getSymbol());
641 }
642
643 // Create an MCSymbol for the reference.
644 MCSymbol *MCSym = OutContext.GetOrCreateSymbol(OS.str());
645 const MCExpr *Expr = MCSymbolRefExpr::Create(MCSym, OutContext);
646
647 // FIXME: Model the whole expression an an MCExpr and we can get rid
648 // of this hasRawTextSupport() clause and just do an EmitValue().
649 if (OutStreamer.hasRawTextSupport()) {
Jim Grosbach3a2429a2010-11-09 21:36:17 +0000650 if (ACPV->hasModifier()) OS << "(" << ACPV->getModifierText() << ")";
Jim Grosbach5df08d82010-11-09 18:45:04 +0000651 if (ACPV->getPCAdjustment() != 0) {
652 OS << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
653 << getFunctionNumber() << "_" << ACPV->getLabelId()
654 << "+" << (unsigned)ACPV->getPCAdjustment();
655 if (ACPV->mustAddCurrentAddress())
656 OS << "-.";
657 OS << ')';
658 }
659 const char *DataDirective = 0;
660 switch (Size) {
661 case 1: DataDirective = MAI->getData8bitsDirective(0); break;
662 case 2: DataDirective = MAI->getData16bitsDirective(0); break;
663 case 4: DataDirective = MAI->getData32bitsDirective(0); break;
664 default: assert(0 && "Unknown CPV size");
665 }
666 Twine Text(DataDirective, OS.str());
667 OutStreamer.EmitRawText(Text);
668 } else {
Jim Grosbachc9962ac2010-11-09 19:40:22 +0000669 assert(!ACPV->hasModifier() &&
Jim Grosbach5df08d82010-11-09 18:45:04 +0000670 "ARM binary streamer of non-trivial constant pool value!");
671 if (ACPV->getPCAdjustment()) {
672 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
673 getFunctionNumber(),
674 ACPV->getLabelId(),
675 OutContext);
676 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
677 PCRelExpr =
678 MCBinaryExpr::CreateAdd(PCRelExpr,
679 MCConstantExpr::Create(ACPV->getPCAdjustment(),
680 OutContext),
681 OutContext);
Jim Grosbachc9962ac2010-11-09 19:40:22 +0000682 if (ACPV->mustAddCurrentAddress()) {
683 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
684 // label, so just emit a local label end reference that instead.
685 MCSymbol *DotSym = OutContext.CreateTempSymbol();
686 OutStreamer.EmitLabel(DotSym);
687 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
688 Expr = MCBinaryExpr::CreateSub(Expr, DotExpr, OutContext);
689 }
Jim Grosbach5df08d82010-11-09 18:45:04 +0000690 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
691 }
692 OutStreamer.EmitValue(Expr, Size);
693 }
694}
695
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000696void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
697 unsigned Opcode = MI->getOpcode();
698 int OpNum = 1;
699 if (Opcode == ARM::BR_JTadd)
700 OpNum = 2;
701 else if (Opcode == ARM::BR_JTm)
702 OpNum = 3;
703
704 const MachineOperand &MO1 = MI->getOperand(OpNum);
705 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
706 unsigned JTI = MO1.getIndex();
707
708 // Emit a label for the jump table.
709 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
710 OutStreamer.EmitLabel(JTISymbol);
711
712 // Emit each entry of the table.
713 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
714 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
715 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
716
717 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
718 MachineBasicBlock *MBB = JTBBs[i];
719 // Construct an MCExpr for the entry. We want a value of the form:
720 // (BasicBlockAddr - TableBeginAddr)
721 //
722 // For example, a table with entries jumping to basic blocks BB0 and BB1
723 // would look like:
724 // LJTI_0_0:
725 // .word (LBB0 - LJTI_0_0)
726 // .word (LBB1 - LJTI_0_0)
727 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
728
729 if (TM.getRelocationModel() == Reloc::PIC_)
730 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
731 OutContext),
732 OutContext);
733 OutStreamer.EmitValue(Expr, 4);
734 }
735}
736
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000737void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
738 unsigned Opcode = MI->getOpcode();
739 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
740 const MachineOperand &MO1 = MI->getOperand(OpNum);
741 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
742 unsigned JTI = MO1.getIndex();
743
744 // Emit a label for the jump table.
745 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
746 OutStreamer.EmitLabel(JTISymbol);
747
748 // Emit each entry of the table.
749 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
750 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
751 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000752 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000753 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000754 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000755 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000756 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000757
758 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
759 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000760 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
761 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000762 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000763 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000764 MCInst BrInst;
765 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000766 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000767 OutStreamer.EmitInstruction(BrInst);
768 continue;
769 }
770 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000771 // MCExpr for the entry. We want a value of the form:
772 // (BasicBlockAddr - TableBeginAddr) / 2
773 //
774 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
775 // would look like:
776 // LJTI_0_0:
777 // .byte (LBB0 - LJTI_0_0) / 2
778 // .byte (LBB1 - LJTI_0_0) / 2
779 const MCExpr *Expr =
780 MCBinaryExpr::CreateSub(MBBSymbolExpr,
781 MCSymbolRefExpr::Create(JTISymbol, OutContext),
782 OutContext);
783 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
784 OutContext);
785 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000786 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000787
788 // Make sure the instruction that follows TBB is 2-byte aligned.
789 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
790 if (MI->getOpcode() == ARM::t2TBB)
791 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000792}
793
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000794void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
795 raw_ostream &OS) {
796 unsigned NOps = MI->getNumOperands();
797 assert(NOps==4);
798 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
799 // cast away const; DIetc do not take const operands for some reason.
800 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
801 OS << V.getName();
802 OS << " <- ";
803 // Frame address. Currently handles register +- offset only.
804 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
805 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
806 OS << ']';
807 OS << "+";
808 printOperand(MI, NOps-2, OS);
809}
810
Jim Grosbachb454cda2010-09-29 15:23:40 +0000811void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000812 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000813 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000814 case ARM::t2MOVi32imm:
815 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000816 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000817 case ARM::DBG_VALUE: {
818 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
819 SmallString<128> TmpStr;
820 raw_svector_ostream OS(TmpStr);
821 PrintDebugValueComment(MI, OS);
822 OutStreamer.EmitRawText(StringRef(OS.str()));
823 }
824 return;
825 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000826 case ARM::tPICADD: {
827 // This is a pseudo op for a label + instruction sequence, which looks like:
828 // LPC0:
829 // add r0, pc
830 // This adds the address of LPC0 to r0.
831
832 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000833 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
834 getFunctionNumber(), MI->getOperand(2).getImm(),
835 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000836
837 // Form and emit the add.
838 MCInst AddInst;
839 AddInst.setOpcode(ARM::tADDhirr);
840 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
841 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
842 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
843 // Add predicate operands.
844 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
845 AddInst.addOperand(MCOperand::CreateReg(0));
846 OutStreamer.EmitInstruction(AddInst);
847 return;
848 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000849 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +0000850 // This is a pseudo op for a label + instruction sequence, which looks like:
851 // LPC0:
852 // add r0, pc, r0
853 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000854
Chris Lattner4d152222009-10-19 22:23:04 +0000855 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000856 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
857 getFunctionNumber(), MI->getOperand(2).getImm(),
858 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000859
Jim Grosbachf3f09522010-09-14 21:05:34 +0000860 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000861 MCInst AddInst;
862 AddInst.setOpcode(ARM::ADDrr);
863 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
864 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
865 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000866 // Add predicate operands.
867 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
868 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
869 // Add 's' bit operand (always reg0 for this)
870 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000871 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000872 return;
873 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000874 case ARM::PICSTR:
875 case ARM::PICSTRB:
876 case ARM::PICSTRH:
877 case ARM::PICLDR:
878 case ARM::PICLDRB:
879 case ARM::PICLDRH:
880 case ARM::PICLDRSB:
881 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000882 // This is a pseudo op for a label + instruction sequence, which looks like:
883 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000884 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000885 // The LCP0 label is referenced by a constant pool entry in order to get
886 // a PC-relative address at the ldr instruction.
887
888 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000889 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
890 getFunctionNumber(), MI->getOperand(2).getImm(),
891 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000892
893 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000894 unsigned Opcode;
895 switch (MI->getOpcode()) {
896 default:
897 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000898 case ARM::PICSTR: Opcode = ARM::STRrs; break;
899 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000900 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000901 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +0000902 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000903 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
904 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
905 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
906 }
907 MCInst LdStInst;
908 LdStInst.setOpcode(Opcode);
909 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
910 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
911 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
912 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000913 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000914 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
915 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
916 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000917
918 return;
919 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000920 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +0000921 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
922 /// in the function. The first operand is the ID# for this instruction, the
923 /// second is the index into the MachineConstantPool that this is, the third
924 /// is the size in bytes of this constant pool entry.
925 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
926 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
927
928 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000929 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000930
931 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
932 if (MCPE.isMachineConstantPoolEntry())
933 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
934 else
935 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000936
Chris Lattnera70e6442009-10-19 22:33:05 +0000937 return;
938 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000939 case ARM::t2TBB:
940 case ARM::t2TBH:
941 case ARM::t2BR_JT: {
942 // Lower and emit the instruction itself, then the jump table following it.
943 MCInst TmpInst;
944 MCInstLowering.Lower(MI, TmpInst);
945 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000946 EmitJump2Table(MI);
947 return;
948 }
949 case ARM::tBR_JTr:
950 case ARM::BR_JTr:
951 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000952 case ARM::BR_JTadd: {
953 // Lower and emit the instruction itself, then the jump table following it.
954 MCInst TmpInst;
955 MCInstLowering.Lower(MI, TmpInst);
956 OutStreamer.EmitInstruction(TmpInst);
957 EmitJumpTable(MI);
958 return;
959 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000960 case ARM::TRAP: {
961 // Non-Darwin binutils don't yet support the "trap" mnemonic.
962 // FIXME: Remove this special case when they do.
963 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000964 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000965 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000966 OutStreamer.AddComment("trap");
967 OutStreamer.EmitIntValue(Val, 4);
968 return;
969 }
970 break;
971 }
972 case ARM::tTRAP: {
973 // Non-Darwin binutils don't yet support the "trap" mnemonic.
974 // FIXME: Remove this special case when they do.
975 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000976 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000977 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000978 OutStreamer.AddComment("trap");
979 OutStreamer.EmitIntValue(Val, 2);
980 return;
981 }
982 break;
983 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000984 case ARM::t2Int_eh_sjlj_setjmp:
985 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000986 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +0000987 // Two incoming args: GPR:$src, GPR:$val
988 // mov $val, pc
989 // adds $val, #7
990 // str $val, [$src, #4]
991 // movs r0, #0
992 // b 1f
993 // movs r0, #1
994 // 1:
995 unsigned SrcReg = MI->getOperand(0).getReg();
996 unsigned ValReg = MI->getOperand(1).getReg();
997 MCSymbol *Label = GetARMSJLJEHLabel();
998 {
999 MCInst TmpInst;
1000 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
1001 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1002 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1003 // 's' bit operand
1004 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1005 OutStreamer.AddComment("eh_setjmp begin");
1006 OutStreamer.EmitInstruction(TmpInst);
1007 }
1008 {
1009 MCInst TmpInst;
1010 TmpInst.setOpcode(ARM::tADDi3);
1011 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1012 // 's' bit operand
1013 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1014 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1015 TmpInst.addOperand(MCOperand::CreateImm(7));
1016 // Predicate.
1017 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1018 TmpInst.addOperand(MCOperand::CreateReg(0));
1019 OutStreamer.EmitInstruction(TmpInst);
1020 }
1021 {
1022 MCInst TmpInst;
1023 TmpInst.setOpcode(ARM::tSTR);
1024 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1025 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1026 // The offset immediate is #4. The operand value is scaled by 4 for the
1027 // tSTR instruction.
1028 TmpInst.addOperand(MCOperand::CreateImm(1));
1029 TmpInst.addOperand(MCOperand::CreateReg(0));
1030 // Predicate.
1031 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1032 TmpInst.addOperand(MCOperand::CreateReg(0));
1033 OutStreamer.EmitInstruction(TmpInst);
1034 }
1035 {
1036 MCInst TmpInst;
1037 TmpInst.setOpcode(ARM::tMOVi8);
1038 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1039 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1040 TmpInst.addOperand(MCOperand::CreateImm(0));
1041 // Predicate.
1042 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1043 TmpInst.addOperand(MCOperand::CreateReg(0));
1044 OutStreamer.EmitInstruction(TmpInst);
1045 }
1046 {
1047 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1048 MCInst TmpInst;
1049 TmpInst.setOpcode(ARM::tB);
1050 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1051 OutStreamer.EmitInstruction(TmpInst);
1052 }
1053 {
1054 MCInst TmpInst;
1055 TmpInst.setOpcode(ARM::tMOVi8);
1056 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1057 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1058 TmpInst.addOperand(MCOperand::CreateImm(1));
1059 // Predicate.
1060 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1061 TmpInst.addOperand(MCOperand::CreateReg(0));
1062 OutStreamer.AddComment("eh_setjmp end");
1063 OutStreamer.EmitInstruction(TmpInst);
1064 }
1065 OutStreamer.EmitLabel(Label);
1066 return;
1067 }
1068
Jim Grosbach45390082010-09-23 23:33:56 +00001069 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001070 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001071 // Two incoming args: GPR:$src, GPR:$val
1072 // add $val, pc, #8
1073 // str $val, [$src, #+4]
1074 // mov r0, #0
1075 // add pc, pc, #0
1076 // mov r0, #1
1077 unsigned SrcReg = MI->getOperand(0).getReg();
1078 unsigned ValReg = MI->getOperand(1).getReg();
1079
1080 {
1081 MCInst TmpInst;
1082 TmpInst.setOpcode(ARM::ADDri);
1083 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1084 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1085 TmpInst.addOperand(MCOperand::CreateImm(8));
1086 // Predicate.
1087 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1088 TmpInst.addOperand(MCOperand::CreateReg(0));
1089 // 's' bit operand (always reg0 for this).
1090 TmpInst.addOperand(MCOperand::CreateReg(0));
1091 OutStreamer.AddComment("eh_setjmp begin");
1092 OutStreamer.EmitInstruction(TmpInst);
1093 }
1094 {
1095 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001096 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001097 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1098 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001099 TmpInst.addOperand(MCOperand::CreateImm(4));
1100 // Predicate.
1101 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1102 TmpInst.addOperand(MCOperand::CreateReg(0));
1103 OutStreamer.EmitInstruction(TmpInst);
1104 }
1105 {
1106 MCInst TmpInst;
1107 TmpInst.setOpcode(ARM::MOVi);
1108 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1109 TmpInst.addOperand(MCOperand::CreateImm(0));
1110 // Predicate.
1111 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1112 TmpInst.addOperand(MCOperand::CreateReg(0));
1113 // 's' bit operand (always reg0 for this).
1114 TmpInst.addOperand(MCOperand::CreateReg(0));
1115 OutStreamer.EmitInstruction(TmpInst);
1116 }
1117 {
1118 MCInst TmpInst;
1119 TmpInst.setOpcode(ARM::ADDri);
1120 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1121 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1122 TmpInst.addOperand(MCOperand::CreateImm(0));
1123 // Predicate.
1124 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1125 TmpInst.addOperand(MCOperand::CreateReg(0));
1126 // 's' bit operand (always reg0 for this).
1127 TmpInst.addOperand(MCOperand::CreateReg(0));
1128 OutStreamer.EmitInstruction(TmpInst);
1129 }
1130 {
1131 MCInst TmpInst;
1132 TmpInst.setOpcode(ARM::MOVi);
1133 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1134 TmpInst.addOperand(MCOperand::CreateImm(1));
1135 // Predicate.
1136 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1137 TmpInst.addOperand(MCOperand::CreateReg(0));
1138 // 's' bit operand (always reg0 for this).
1139 TmpInst.addOperand(MCOperand::CreateReg(0));
1140 OutStreamer.AddComment("eh_setjmp end");
1141 OutStreamer.EmitInstruction(TmpInst);
1142 }
1143 return;
1144 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001145 case ARM::Int_eh_sjlj_longjmp: {
1146 // ldr sp, [$src, #8]
1147 // ldr $scratch, [$src, #4]
1148 // ldr r7, [$src]
1149 // bx $scratch
1150 unsigned SrcReg = MI->getOperand(0).getReg();
1151 unsigned ScratchReg = MI->getOperand(1).getReg();
1152 {
1153 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001154 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001155 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1156 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001157 TmpInst.addOperand(MCOperand::CreateImm(8));
1158 // Predicate.
1159 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1160 TmpInst.addOperand(MCOperand::CreateReg(0));
1161 OutStreamer.EmitInstruction(TmpInst);
1162 }
1163 {
1164 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001165 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001166 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1167 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001168 TmpInst.addOperand(MCOperand::CreateImm(4));
1169 // Predicate.
1170 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1171 TmpInst.addOperand(MCOperand::CreateReg(0));
1172 OutStreamer.EmitInstruction(TmpInst);
1173 }
1174 {
1175 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001176 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001177 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1178 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001179 TmpInst.addOperand(MCOperand::CreateImm(0));
1180 // Predicate.
1181 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1182 TmpInst.addOperand(MCOperand::CreateReg(0));
1183 OutStreamer.EmitInstruction(TmpInst);
1184 }
1185 {
1186 MCInst TmpInst;
1187 TmpInst.setOpcode(ARM::BRIND);
1188 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1189 // Predicate.
1190 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1191 TmpInst.addOperand(MCOperand::CreateReg(0));
1192 OutStreamer.EmitInstruction(TmpInst);
1193 }
1194 return;
1195 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001196 case ARM::tInt_eh_sjlj_longjmp: {
1197 // ldr $scratch, [$src, #8]
1198 // mov sp, $scratch
1199 // ldr $scratch, [$src, #4]
1200 // ldr r7, [$src]
1201 // bx $scratch
1202 unsigned SrcReg = MI->getOperand(0).getReg();
1203 unsigned ScratchReg = MI->getOperand(1).getReg();
1204 {
1205 MCInst TmpInst;
1206 TmpInst.setOpcode(ARM::tLDR);
1207 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1208 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1209 // The offset immediate is #8. The operand value is scaled by 4 for the
1210 // tSTR instruction.
1211 TmpInst.addOperand(MCOperand::CreateImm(2));
1212 TmpInst.addOperand(MCOperand::CreateReg(0));
1213 // Predicate.
1214 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1215 TmpInst.addOperand(MCOperand::CreateReg(0));
1216 OutStreamer.EmitInstruction(TmpInst);
1217 }
1218 {
1219 MCInst TmpInst;
1220 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1221 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1222 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1223 // Predicate.
1224 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1225 TmpInst.addOperand(MCOperand::CreateReg(0));
1226 OutStreamer.EmitInstruction(TmpInst);
1227 }
1228 {
1229 MCInst TmpInst;
1230 TmpInst.setOpcode(ARM::tLDR);
1231 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1232 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1233 TmpInst.addOperand(MCOperand::CreateImm(1));
1234 TmpInst.addOperand(MCOperand::CreateReg(0));
1235 // Predicate.
1236 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1237 TmpInst.addOperand(MCOperand::CreateReg(0));
1238 OutStreamer.EmitInstruction(TmpInst);
1239 }
1240 {
1241 MCInst TmpInst;
1242 TmpInst.setOpcode(ARM::tLDR);
1243 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1244 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1245 TmpInst.addOperand(MCOperand::CreateImm(0));
1246 TmpInst.addOperand(MCOperand::CreateReg(0));
1247 // Predicate.
1248 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1249 TmpInst.addOperand(MCOperand::CreateReg(0));
1250 OutStreamer.EmitInstruction(TmpInst);
1251 }
1252 {
1253 MCInst TmpInst;
1254 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1255 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1256 // Predicate.
1257 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1258 TmpInst.addOperand(MCOperand::CreateReg(0));
1259 OutStreamer.EmitInstruction(TmpInst);
1260 }
1261 return;
1262 }
Chris Lattner97f06932009-10-19 20:20:46 +00001263 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001264
Chris Lattner97f06932009-10-19 20:20:46 +00001265 MCInst TmpInst;
1266 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001267 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001268}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001269
1270//===----------------------------------------------------------------------===//
1271// Target Registry Stuff
1272//===----------------------------------------------------------------------===//
1273
1274static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1275 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001276 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001277 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001278 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001279 return 0;
1280}
1281
1282// Force static initialization.
1283extern "C" void LLVMInitializeARMAsmPrinter() {
1284 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1285 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1286
1287 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1288 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1289}
1290