blob: 81a36d16d77ed46b59abd5fd19d22538b71b0558 [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"
Jim Grosbachbaf120f2010-12-01 03:45:07 +000017#include "ARMAsmPrinter.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000018#include "ARMBuildAttrs.h"
19#include "ARMBaseRegisterInfo.h"
20#include "ARMConstantPoolValue.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
Chris Lattner97f06932009-10-19 20:20:46 +000022#include "ARMTargetMachine.h"
Jason W Kim17b443d2010-10-11 23:01:44 +000023#include "ARMTargetObjectFile.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000024#include "InstPrinter/ARMInstPrinter.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
26#include "MCTargetDesc/ARMMCExpr.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000027#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000030#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000031#include "llvm/Assembly/Writer.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000032#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000035#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000036#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000037#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000038#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000039#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000040#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000041#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000042#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000043#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000044#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000045#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000046#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000047#include "llvm/Target/TargetOptions.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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000054#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000055#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000057using namespace llvm;
58
Chris Lattner95b2c7d2006-12-19 22:59:26 +000059namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000060
61 // Per section and per symbol attributes are not supported.
62 // To implement them we would need the ability to delay this emission
63 // until the assembly file is fully parsed/generated as only then do we
64 // know the symbol and section numbers.
65 class AttributeEmitter {
66 public:
67 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
68 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
Jason W Kimf009a962011-02-07 00:49:53 +000069 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000070 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000071 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000072 };
73
74 class AsmAttributeEmitter : public AttributeEmitter {
75 MCStreamer &Streamer;
76
77 public:
78 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
79 void MaybeSwitchVendor(StringRef Vendor) { }
80
81 void EmitAttribute(unsigned Attribute, unsigned Value) {
82 Streamer.EmitRawText("\t.eabi_attribute " +
83 Twine(Attribute) + ", " + Twine(Value));
84 }
85
Jason W Kimf009a962011-02-07 00:49:53 +000086 void EmitTextAttribute(unsigned Attribute, StringRef String) {
87 switch (Attribute) {
88 case ARMBuildAttrs::CPU_name:
Jason W Kimc046d642011-02-07 19:07:11 +000089 Streamer.EmitRawText(StringRef("\t.cpu ") + LowercaseString(String));
Jason W Kimf009a962011-02-07 00:49:53 +000090 break;
Renato Golin728ff0d2011-02-28 22:04:27 +000091 /* GAS requires .fpu to be emitted regardless of EABI attribute */
92 case ARMBuildAttrs::Advanced_SIMD_arch:
93 case ARMBuildAttrs::VFP_arch:
94 Streamer.EmitRawText(StringRef("\t.fpu ") + LowercaseString(String));
Jim Grosbach8e0c7692011-09-02 18:46:15 +000095 break;
Jason W Kimf009a962011-02-07 00:49:53 +000096 default: assert(0 && "Unsupported Text attribute in ASM Mode"); break;
97 }
98 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000099 void Finish() { }
100 };
101
102 class ObjectAttributeEmitter : public AttributeEmitter {
Renato Golin719927a2011-08-09 09:50:10 +0000103 // This structure holds all attributes, accounting for
104 // their string/numeric value, so we can later emmit them
105 // in declaration order, keeping all in the same vector
106 struct AttributeItemType {
107 enum {
108 HiddenAttribute = 0,
109 NumericAttribute,
110 TextAttribute
111 } Type;
112 unsigned Tag;
113 unsigned IntValue;
114 StringRef StringValue;
115 } AttributeItem;
116
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000117 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000118 StringRef CurrentVendor;
Renato Golin719927a2011-08-09 09:50:10 +0000119 SmallVector<AttributeItemType, 64> Contents;
120
121 // Account for the ULEB/String size of each item,
122 // not just the number of items
123 size_t ContentsSize;
124 // FIXME: this should be in a more generic place, but
125 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
126 size_t getULEBSize(int Value) {
127 size_t Size = 0;
128 do {
129 Value >>= 7;
130 Size += sizeof(int8_t); // Is this really necessary?
131 } while (Value);
132 return Size;
133 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000134
135 public:
136 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
Renato Golin719927a2011-08-09 09:50:10 +0000137 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000138
139 void MaybeSwitchVendor(StringRef Vendor) {
140 assert(!Vendor.empty() && "Vendor cannot be empty.");
141
142 if (CurrentVendor.empty())
143 CurrentVendor = Vendor;
144 else if (CurrentVendor == Vendor)
145 return;
146 else
147 Finish();
148
149 CurrentVendor = Vendor;
150
Rafael Espindola33363842010-10-25 22:26:55 +0000151 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000152 }
153
154 void EmitAttribute(unsigned Attribute, unsigned Value) {
Renato Golin719927a2011-08-09 09:50:10 +0000155 AttributeItemType attr = {
156 AttributeItemType::NumericAttribute,
157 Attribute,
158 Value,
159 StringRef("")
160 };
161 ContentsSize += getULEBSize(Attribute);
162 ContentsSize += getULEBSize(Value);
163 Contents.push_back(attr);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000164 }
165
Jason W Kimf009a962011-02-07 00:49:53 +0000166 void EmitTextAttribute(unsigned Attribute, StringRef String) {
Renato Golin719927a2011-08-09 09:50:10 +0000167 AttributeItemType attr = {
168 AttributeItemType::TextAttribute,
169 Attribute,
170 0,
171 String
172 };
173 ContentsSize += getULEBSize(Attribute);
174 // String + \0
175 ContentsSize += String.size()+1;
176
177 Contents.push_back(attr);
Jason W Kimf009a962011-02-07 00:49:53 +0000178 }
179
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000180 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000181 // Vendor size + Vendor name + '\0'
182 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000183
Rafael Espindola33363842010-10-25 22:26:55 +0000184 // Tag + Tag Size
185 const size_t TagHeaderSize = 1 + 4;
186
187 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
188 Streamer.EmitBytes(CurrentVendor, 0);
189 Streamer.EmitIntValue(0, 1); // '\0'
190
191 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
192 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000193
Renato Golin719927a2011-08-09 09:50:10 +0000194 // Size should have been accounted for already, now
195 // emit each field as its type (ULEB or String)
196 for (unsigned int i=0; i<Contents.size(); ++i) {
197 AttributeItemType item = Contents[i];
198 Streamer.EmitULEB128IntValue(item.Tag, 0);
199 switch (item.Type) {
200 case AttributeItemType::NumericAttribute:
201 Streamer.EmitULEB128IntValue(item.IntValue, 0);
202 break;
203 case AttributeItemType::TextAttribute:
204 Streamer.EmitBytes(UppercaseString(item.StringValue), 0);
205 Streamer.EmitIntValue(0, 1); // '\0'
206 break;
207 default:
208 assert(0 && "Invalid attribute type");
209 }
210 }
Rafael Espindola33363842010-10-25 22:26:55 +0000211
212 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000213 }
214 };
215
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000216} // end of anonymous namespace
217
Jim Grosbachbaf120f2010-12-01 03:45:07 +0000218MachineLocation ARMAsmPrinter::
219getDebugValueLocation(const MachineInstr *MI) const {
220 MachineLocation Location;
221 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
222 // Frame address. Currently handles register +- offset only.
223 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
224 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
225 else {
226 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
227 }
228 return Location;
229}
230
Devang Patel27f5acb2011-04-21 22:48:26 +0000231/// EmitDwarfRegOp - Emit dwarf register operation.
Devang Patel0be77df2011-04-27 20:29:27 +0000232void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc) const {
Devang Patel27f5acb2011-04-21 22:48:26 +0000233 const TargetRegisterInfo *RI = TM.getRegisterInfo();
234 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1)
Devang Patel0be77df2011-04-27 20:29:27 +0000235 AsmPrinter::EmitDwarfRegOp(MLoc);
Devang Patel27f5acb2011-04-21 22:48:26 +0000236 else {
237 unsigned Reg = MLoc.getReg();
238 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000239 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
Devang Patel27f5acb2011-04-21 22:48:26 +0000240 // S registers are described as bit-pieces of a register
241 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
242 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000243
Devang Patel27f5acb2011-04-21 22:48:26 +0000244 unsigned SReg = Reg - ARM::S0;
245 bool odd = SReg & 0x1;
246 unsigned Rx = 256 + (SReg >> 1);
Devang Patel27f5acb2011-04-21 22:48:26 +0000247
248 OutStreamer.AddComment("DW_OP_regx for S register");
249 EmitInt8(dwarf::DW_OP_regx);
250
251 OutStreamer.AddComment(Twine(SReg));
252 EmitULEB128(Rx);
253
254 if (odd) {
255 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
256 EmitInt8(dwarf::DW_OP_bit_piece);
257 EmitULEB128(32);
258 EmitULEB128(32);
259 } else {
260 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
261 EmitInt8(dwarf::DW_OP_bit_piece);
262 EmitULEB128(32);
263 EmitULEB128(0);
264 }
Devang Patel71f3f112011-04-21 23:22:35 +0000265 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000266 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
Devang Patel71f3f112011-04-21 23:22:35 +0000267 // Q registers Q0-Q15 are described by composing two D registers together.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000268 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
269 // DW_OP_piece(8)
Devang Patel71f3f112011-04-21 23:22:35 +0000270
271 unsigned QReg = Reg - ARM::Q0;
272 unsigned D1 = 256 + 2 * QReg;
273 unsigned D2 = D1 + 1;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000274
Devang Patel71f3f112011-04-21 23:22:35 +0000275 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
276 EmitInt8(dwarf::DW_OP_regx);
277 EmitULEB128(D1);
278 OutStreamer.AddComment("DW_OP_piece 8");
279 EmitInt8(dwarf::DW_OP_piece);
280 EmitULEB128(8);
281
282 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
283 EmitInt8(dwarf::DW_OP_regx);
284 EmitULEB128(D2);
285 OutStreamer.AddComment("DW_OP_piece 8");
286 EmitInt8(dwarf::DW_OP_piece);
287 EmitULEB128(8);
Devang Patel27f5acb2011-04-21 22:48:26 +0000288 }
289 }
290}
291
Chris Lattner953ebb72010-01-27 23:58:11 +0000292void ARMAsmPrinter::EmitFunctionEntryLabel() {
293 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000294 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000295 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000296 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000297
Chris Lattner953ebb72010-01-27 23:58:11 +0000298 OutStreamer.EmitLabel(CurrentFnSym);
299}
300
Jim Grosbach2317e402010-09-30 01:57:53 +0000301/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000302/// method to print assembly for each instruction.
303///
304bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000305 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000306 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000307
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000308 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000309}
310
Evan Cheng055b0312009-06-29 07:51:04 +0000311void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000312 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000313 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000314 unsigned TF = MO.getTargetFlags();
315
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000316 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000317 default:
318 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000319 case MachineOperand::MO_Register: {
320 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000321 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000322 assert(!MO.getSubReg() && "Subregs should be eliminated!");
323 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000324 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000325 }
Evan Chenga8e29892007-01-19 07:51:42 +0000326 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000327 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000328 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000329 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000330 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000331 O << ":lower16:";
332 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000333 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000334 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000335 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000336 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000337 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000338 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000339 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000340 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000341 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000342 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000343 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
344 (TF & ARMII::MO_LO16))
345 O << ":lower16:";
346 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
347 (TF & ARMII::MO_HI16))
348 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000349 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000350
Chris Lattner0c08d092010-04-03 22:28:33 +0000351 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000352 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000353 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000354 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000355 }
Evan Chenga8e29892007-01-19 07:51:42 +0000356 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000357 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000358 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000359 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000360 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000361 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000362 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000363 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000364 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000365 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000366 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000367 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000368 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000369}
370
Evan Cheng055b0312009-06-29 07:51:04 +0000371//===--------------------------------------------------------------------===//
372
Chris Lattner0890cf12010-01-25 19:51:38 +0000373MCSymbol *ARMAsmPrinter::
374GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
375 const MachineBasicBlock *MBB) const {
376 SmallString<60> Name;
377 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000378 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000379 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000380 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000381}
382
383MCSymbol *ARMAsmPrinter::
384GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
385 SmallString<60> Name;
386 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000387 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000388 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000389}
390
Jim Grosbach433a5782010-09-24 20:47:58 +0000391
392MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
393 SmallString<60> Name;
394 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
395 << getFunctionNumber();
396 return OutContext.GetOrCreateSymbol(Name.str());
397}
398
Evan Cheng055b0312009-06-29 07:51:04 +0000399bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000400 unsigned AsmVariant, const char *ExtraCode,
401 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000402 // Does this asm operand have a single letter operand modifier?
403 if (ExtraCode && ExtraCode[0]) {
404 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000405
Evan Chenga8e29892007-01-19 07:51:42 +0000406 switch (ExtraCode[0]) {
407 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000408 case 'a': // Print as a memory address.
409 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000410 O << "["
411 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
412 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000413 return false;
414 }
415 // Fallthrough
416 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000417 if (!MI->getOperand(OpNum).isImm())
418 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000419 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000420 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000421 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000422 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000423 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000424 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000425 case 'y': // Print a VFP single precision register as indexed double.
426 // This uses the ordering of the alias table to get the first 'd' register
427 // that overlaps the 's' register. Also, s0 is an odd register, hence the
428 // odd modulus check below.
429 if (MI->getOperand(OpNum).isReg()) {
430 unsigned Reg = MI->getOperand(OpNum).getReg();
431 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
432 O << ARMInstPrinter::getRegisterName(TRI->getAliasSet(Reg)[0]) <<
433 (((Reg % 2) == 1) ? "[0]" : "[1]");
434 return false;
435 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000436 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000437 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000438 if (!MI->getOperand(OpNum).isImm())
439 return true;
440 O << ~(MI->getOperand(OpNum).getImm());
441 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000442 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000443 if (!MI->getOperand(OpNum).isImm())
444 return true;
445 O << (MI->getOperand(OpNum).getImm() & 0xffff);
446 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000447 case 'M': { // A register range suitable for LDM/STM.
448 if (!MI->getOperand(OpNum).isReg())
449 return true;
450 const MachineOperand &MO = MI->getOperand(OpNum);
451 unsigned RegBegin = MO.getReg();
452 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
453 // already got the operands in registers that are operands to the
454 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000455
Eric Christopher3c14f242011-05-28 01:40:44 +0000456 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000457
Eric Christopher3c14f242011-05-28 01:40:44 +0000458 // FIXME: The register allocator not only may not have given us the
459 // registers in sequence, but may not be in ascending registers. This
460 // will require changes in the register allocator that'll need to be
461 // propagated down here if the operands change.
462 unsigned RegOps = OpNum + 1;
463 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000464 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000465 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
466 RegOps++;
467 }
468
469 O << "}";
470
471 return false;
472 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000473 case 'R': // The most significant register of a pair.
474 case 'Q': { // The least significant register of a pair.
475 if (OpNum == 0)
476 return true;
477 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
478 if (!FlagsOP.isImm())
479 return true;
480 unsigned Flags = FlagsOP.getImm();
481 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
482 if (NumVals != 2)
483 return true;
484 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
485 if (RegOp >= MI->getNumOperands())
486 return true;
487 const MachineOperand &MO = MI->getOperand(RegOp);
488 if (!MO.isReg())
489 return true;
490 unsigned Reg = MO.getReg();
491 O << ARMInstPrinter::getRegisterName(Reg);
492 return false;
493 }
494
Eric Christopher3c14f242011-05-28 01:40:44 +0000495 // These modifiers are not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000496 case 'p': // The high single-precision register of a VFP double-precision
497 // register.
498 case 'e': // The low doubleword register of a NEON quad register.
499 case 'f': // The high doubleword register of a NEON quad register.
500 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Eric Christopherfef50062011-05-24 22:27:43 +0000501 case 'H': // The highest-numbered register of a pair.
Bob Wilsond984eb62010-05-27 20:23:42 +0000502 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000503 }
Evan Chenga8e29892007-01-19 07:51:42 +0000504 }
Jim Grosbache9952212009-09-04 01:38:51 +0000505
Chris Lattner35c33bd2010-04-04 04:47:45 +0000506 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000507 return false;
508}
509
Bob Wilson224c2442009-05-19 05:53:42 +0000510bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000511 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000512 const char *ExtraCode,
513 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000514 // Does this asm operand have a single letter operand modifier?
515 if (ExtraCode && ExtraCode[0]) {
516 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000517
Eric Christopher8f894632011-05-25 20:51:58 +0000518 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000519 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000520 default: return true; // Unknown modifier.
521 case 'm': // The base register of a memory operand.
522 if (!MI->getOperand(OpNum).isReg())
523 return true;
524 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
525 return false;
526 }
527 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000528
Bob Wilson765cc0b2009-10-13 20:50:28 +0000529 const MachineOperand &MO = MI->getOperand(OpNum);
530 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000531 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000532 return false;
533}
534
Bob Wilson812209a2009-09-30 22:06:26 +0000535void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000536 if (Subtarget->isTargetDarwin()) {
537 Reloc::Model RelocM = TM.getRelocationModel();
538 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
539 // Declare all the text sections up front (before the DWARF sections
540 // emitted by AsmPrinter::doInitialization) so the assembler will keep
541 // them together at the beginning of the object file. This helps
542 // avoid out-of-range branches that are due a fundamental limitation of
543 // the way symbol offsets are encoded with the current Darwin ARM
544 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000545 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000546 static_cast<const TargetLoweringObjectFileMachO &>(
547 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000548 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
549 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
550 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
551 if (RelocM == Reloc::DynamicNoPIC) {
552 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000553 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
554 MCSectionMachO::S_SYMBOL_STUBS,
555 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000556 OutStreamer.SwitchSection(sect);
557 } else {
558 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000559 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
560 MCSectionMachO::S_SYMBOL_STUBS,
561 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000562 OutStreamer.SwitchSection(sect);
563 }
Bob Wilson63db5942010-07-30 19:55:47 +0000564 const MCSection *StaticInitSect =
565 OutContext.getMachOSection("__TEXT", "__StaticInit",
566 MCSectionMachO::S_REGULAR |
567 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
568 SectionKind::getText());
569 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000570 }
571 }
572
Jim Grosbache5165492009-11-09 00:11:35 +0000573 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000574 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000575
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000576 // Emit ARM Build Attributes
577 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000578
Jason W Kimdef9ac42010-10-06 22:36:46 +0000579 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000580 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000581}
582
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000583
Chris Lattner4a071d62009-10-19 17:59:19 +0000584void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000585 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000586 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000587 const TargetLoweringObjectFileMachO &TLOFMacho =
588 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000589 MachineModuleInfoMachO &MMIMacho =
590 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000591
Evan Chenga8e29892007-01-19 07:51:42 +0000592 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000593 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000594
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000595 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000597 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000598 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000599 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000600 // L_foo$stub:
601 OutStreamer.EmitLabel(Stubs[i].first);
602 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000603 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
604 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000605
Bill Wendling52a50e52010-03-11 01:18:13 +0000606 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000607 // External to current translation unit.
608 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
609 else
610 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000611 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000612 // When we place the LSDA into the TEXT section, the type info
613 // pointers need to be indirect and pc-rel. We accomplish this by
614 // using NLPs; however, sometimes the types are local to the file.
615 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000616 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
617 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000618 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000619 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000620
621 Stubs.clear();
622 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000623 }
624
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000625 Stubs = MMIMacho.GetHiddenGVStubList();
626 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000627 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000628 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000629 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
630 // L_foo$stub:
631 OutStreamer.EmitLabel(Stubs[i].first);
632 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000633 OutStreamer.EmitValue(MCSymbolRefExpr::
634 Create(Stubs[i].second.getPointer(),
635 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000636 4/*size*/, 0/*addrspace*/);
637 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000638
639 Stubs.clear();
640 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000641 }
642
Evan Chenga8e29892007-01-19 07:51:42 +0000643 // Funny Darwin hack: This flag tells the linker that no global symbols
644 // contain code that falls through to other global symbols (e.g. the obvious
645 // implementation of multiple entry points). If this doesn't occur, the
646 // linker can safely perform dead code stripping. Since LLVM never
647 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000648 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000649 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000650}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000651
Chris Lattner97f06932009-10-19 20:20:46 +0000652//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000653// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
654// FIXME:
655// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000656// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000657// Instead of subclassing the MCELFStreamer, we do the work here.
658
659void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000660
Jason W Kim17b443d2010-10-11 23:01:44 +0000661 emitARMAttributeSection();
662
Renato Golin728ff0d2011-02-28 22:04:27 +0000663 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
664 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000665 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000666 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000667 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000668 emitFPU = true;
669 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000670 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
671 AttrEmitter = new ObjectAttributeEmitter(O);
672 }
673
674 AttrEmitter->MaybeSwitchVendor("aeabi");
675
Jason W Kimdef9ac42010-10-06 22:36:46 +0000676 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000677
678 if (CPUString == "cortex-a8" ||
679 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000680 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000681 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
682 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
683 ARMBuildAttrs::ApplicationProfile);
684 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
685 ARMBuildAttrs::Allowed);
686 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
687 ARMBuildAttrs::AllowThumb32);
688 // Fixme: figure out when this is emitted.
689 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
690 // ARMBuildAttrs::AllowWMMXv1);
691 //
692
693 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000694 } else if (CPUString == "xscale") {
695 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
696 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
697 ARMBuildAttrs::Allowed);
698 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
699 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000700 } else if (CPUString == "generic") {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000701 // FIXME: Why these defaults?
702 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimf009a962011-02-07 00:49:53 +0000703 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
704 ARMBuildAttrs::Allowed);
705 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
706 ARMBuildAttrs::Allowed);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000707 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000708
Renato Goline89a0532011-03-02 21:20:09 +0000709 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000710 /* NEON is not exactly a VFP architecture, but GAS emit one of
711 * neon/vfpv3/vfpv2 for .fpu parameters */
712 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
713 /* If emitted for NEON, omit from VFP below, since you can have both
714 * NEON and VFP in build attributes but only one .fpu */
715 emitFPU = false;
716 }
717
718 /* VFPv3 + .fpu */
719 if (Subtarget->hasVFP3()) {
720 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
721 ARMBuildAttrs::AllowFPv3A);
722 if (emitFPU)
723 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
724
725 /* VFPv2 + .fpu */
726 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000727 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
728 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000729 if (emitFPU)
730 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
731 }
732
733 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000734 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000735 if (Subtarget->hasNEON()) {
736 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
737 ARMBuildAttrs::Allowed);
738 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000739
740 // Signal various FP modes.
741 if (!UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000742 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
743 ARMBuildAttrs::Allowed);
744 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
745 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000746 }
747
748 if (NoInfsFPMath && NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000749 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
750 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000751 else
Jason W Kimf009a962011-02-07 00:49:53 +0000752 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
753 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000754
Jason W Kimf009a962011-02-07 00:49:53 +0000755 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000756 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000757 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
758 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000759
760 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
761 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000762 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
763 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000764 }
765 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000766
Jason W Kimf009a962011-02-07 00:49:53 +0000767 if (Subtarget->hasDivide())
768 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000769
770 AttrEmitter->Finish();
771 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000772}
773
Jason W Kim17b443d2010-10-11 23:01:44 +0000774void ARMAsmPrinter::emitARMAttributeSection() {
775 // <format-version>
776 // [ <section-length> "vendor-name"
777 // [ <file-tag> <size> <attribute>*
778 // | <section-tag> <size> <section-number>* 0 <attribute>*
779 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
780 // ]+
781 // ]*
782
783 if (OutStreamer.hasRawTextSupport())
784 return;
785
786 const ARMElfTargetObjectFile &TLOFELF =
787 static_cast<const ARMElfTargetObjectFile &>
788 (getObjFileLowering());
789
790 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000791
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000792 // Format version
793 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000794}
795
Jason W Kimdef9ac42010-10-06 22:36:46 +0000796//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000797
Jim Grosbach988ce092010-09-18 00:05:05 +0000798static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
799 unsigned LabelId, MCContext &Ctx) {
800
801 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
802 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
803 return Label;
804}
805
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000806static MCSymbolRefExpr::VariantKind
807getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
808 switch (Modifier) {
809 default: llvm_unreachable("Unknown modifier!");
810 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
811 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
812 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
813 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
814 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
815 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
816 }
817 return MCSymbolRefExpr::VK_None;
818}
819
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000820MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
821 bool isIndirect = Subtarget->isTargetDarwin() &&
822 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
823 if (!isIndirect)
824 return Mang->getSymbol(GV);
825
826 // FIXME: Remove this when Darwin transition to @GOT like syntax.
827 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
828 MachineModuleInfoMachO &MMIMachO =
829 MMI->getObjFileInfo<MachineModuleInfoMachO>();
830 MachineModuleInfoImpl::StubValueTy &StubSym =
831 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
832 MMIMachO.getGVStubEntry(MCSym);
833 if (StubSym.getPointer() == 0)
834 StubSym = MachineModuleInfoImpl::
835 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
836 return MCSym;
837}
838
Jim Grosbach5df08d82010-11-09 18:45:04 +0000839void ARMAsmPrinter::
840EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
841 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
842
843 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000844
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000845 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000846 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000847 SmallString<128> Str;
848 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000849 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000850 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000851 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000852 const BlockAddress *BA =
853 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
854 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000855 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000856 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000857 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000858 } else if (ACPV->isMachineBasicBlock()) {
Bill Wendling3320f2a2011-10-01 09:30:42 +0000859 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
Bill Wendlinge00897c2011-09-29 23:50:42 +0000860 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000861 } else {
862 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Bill Wendlingfe31e672011-10-01 08:58:29 +0000863 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
864 MCSym = GetExternalSymbolSymbol(Sym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000865 }
866
867 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000868 const MCExpr *Expr =
869 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
870 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000871
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000872 if (ACPV->getPCAdjustment()) {
873 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
874 getFunctionNumber(),
875 ACPV->getLabelId(),
876 OutContext);
877 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
878 PCRelExpr =
879 MCBinaryExpr::CreateAdd(PCRelExpr,
880 MCConstantExpr::Create(ACPV->getPCAdjustment(),
881 OutContext),
882 OutContext);
883 if (ACPV->mustAddCurrentAddress()) {
884 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
885 // label, so just emit a local label end reference that instead.
886 MCSymbol *DotSym = OutContext.CreateTempSymbol();
887 OutStreamer.EmitLabel(DotSym);
888 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
889 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000890 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000891 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000892 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000893 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000894}
895
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000896void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
897 unsigned Opcode = MI->getOpcode();
898 int OpNum = 1;
899 if (Opcode == ARM::BR_JTadd)
900 OpNum = 2;
901 else if (Opcode == ARM::BR_JTm)
902 OpNum = 3;
903
904 const MachineOperand &MO1 = MI->getOperand(OpNum);
905 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
906 unsigned JTI = MO1.getIndex();
907
908 // Emit a label for the jump table.
909 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
910 OutStreamer.EmitLabel(JTISymbol);
911
912 // Emit each entry of the table.
913 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
914 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
915 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
916
917 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
918 MachineBasicBlock *MBB = JTBBs[i];
919 // Construct an MCExpr for the entry. We want a value of the form:
920 // (BasicBlockAddr - TableBeginAddr)
921 //
922 // For example, a table with entries jumping to basic blocks BB0 and BB1
923 // would look like:
924 // LJTI_0_0:
925 // .word (LBB0 - LJTI_0_0)
926 // .word (LBB1 - LJTI_0_0)
927 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
928
929 if (TM.getRelocationModel() == Reloc::PIC_)
930 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
931 OutContext),
932 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +0000933 // If we're generating a table of Thumb addresses in static relocation
934 // model, we need to add one to keep interworking correctly.
935 else if (AFI->isThumbFunction())
936 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
937 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000938 OutStreamer.EmitValue(Expr, 4);
939 }
940}
941
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000942void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
943 unsigned Opcode = MI->getOpcode();
944 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
945 const MachineOperand &MO1 = MI->getOperand(OpNum);
946 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
947 unsigned JTI = MO1.getIndex();
948
949 // Emit a label for the jump table.
950 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
951 OutStreamer.EmitLabel(JTISymbol);
952
953 // Emit each entry of the table.
954 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
955 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
956 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000957 unsigned OffsetWidth = 4;
Jim Grosbachd092a872010-11-29 21:28:32 +0000958 if (MI->getOpcode() == ARM::t2TBB_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000959 OffsetWidth = 1;
Jim Grosbachd092a872010-11-29 21:28:32 +0000960 else if (MI->getOpcode() == ARM::t2TBH_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000961 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000962
963 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
964 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000965 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
966 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000967 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000968 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000969 MCInst BrInst;
970 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000971 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000972 BrInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
973 BrInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000974 OutStreamer.EmitInstruction(BrInst);
975 continue;
976 }
977 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000978 // MCExpr for the entry. We want a value of the form:
979 // (BasicBlockAddr - TableBeginAddr) / 2
980 //
981 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
982 // would look like:
983 // LJTI_0_0:
984 // .byte (LBB0 - LJTI_0_0) / 2
985 // .byte (LBB1 - LJTI_0_0) / 2
986 const MCExpr *Expr =
987 MCBinaryExpr::CreateSub(MBBSymbolExpr,
988 MCSymbolRefExpr::Create(JTISymbol, OutContext),
989 OutContext);
990 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
991 OutContext);
992 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000993 }
994}
995
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000996void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
997 raw_ostream &OS) {
998 unsigned NOps = MI->getNumOperands();
999 assert(NOps==4);
1000 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1001 // cast away const; DIetc do not take const operands for some reason.
1002 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1003 OS << V.getName();
1004 OS << " <- ";
1005 // Frame address. Currently handles register +- offset only.
1006 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1007 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1008 OS << ']';
1009 OS << "+";
1010 printOperand(MI, NOps-2, OS);
1011}
1012
Jim Grosbach40edf732010-12-14 21:10:47 +00001013static void populateADROperands(MCInst &Inst, unsigned Dest,
1014 const MCSymbol *Label,
1015 unsigned pred, unsigned ccreg,
1016 MCContext &Ctx) {
1017 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, Ctx);
1018 Inst.addOperand(MCOperand::CreateReg(Dest));
1019 Inst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1020 // Add predicate operands.
1021 Inst.addOperand(MCOperand::CreateImm(pred));
1022 Inst.addOperand(MCOperand::CreateReg(ccreg));
1023}
1024
Anton Korobeynikov4d728602011-01-01 20:38:38 +00001025void ARMAsmPrinter::EmitPatchedInstruction(const MachineInstr *MI,
1026 unsigned Opcode) {
1027 MCInst TmpInst;
1028
1029 // Emit the instruction as usual, just patch the opcode.
1030 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1031 TmpInst.setOpcode(Opcode);
1032 OutStreamer.EmitInstruction(TmpInst);
1033}
1034
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001035void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1036 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1037 "Only instruction which are involved into frame setup code are allowed");
1038
1039 const MachineFunction &MF = *MI->getParent()->getParent();
1040 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001041 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001042
1043 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001044 unsigned Opc = MI->getOpcode();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001045 unsigned SrcReg, DstReg;
1046
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001047 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1048 // Two special cases:
1049 // 1) tPUSH does not have src/dst regs.
1050 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1051 // load. Yes, this is pretty fragile, but for now I don't see better
1052 // way... :(
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001053 SrcReg = DstReg = ARM::SP;
1054 } else {
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001055 SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001056 DstReg = MI->getOperand(0).getReg();
1057 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001058
1059 // Try to figure out the unwinding opcode out of src / dst regs.
1060 if (MI->getDesc().mayStore()) {
1061 // Register saves.
1062 assert(DstReg == ARM::SP &&
1063 "Only stack pointer as a destination reg is supported");
1064
1065 SmallVector<unsigned, 4> RegList;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001066 // Skip src & dst reg, and pred ops.
1067 unsigned StartOp = 2 + 2;
1068 // Use all the operands.
1069 unsigned NumOffset = 0;
1070
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001071 switch (Opc) {
1072 default:
1073 MI->dump();
1074 assert(0 && "Unsupported opcode for unwinding information");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001075 case ARM::tPUSH:
1076 // Special case here: no src & dst reg, but two extra imp ops.
1077 StartOp = 2; NumOffset = 2;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001078 case ARM::STMDB_UPD:
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001079 case ARM::t2STMDB_UPD:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001080 case ARM::VSTMDDB_UPD:
1081 assert(SrcReg == ARM::SP &&
1082 "Only stack pointer as a source reg is supported");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001083 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1084 i != NumOps; ++i)
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001085 RegList.push_back(MI->getOperand(i).getReg());
1086 break;
Owen Anderson793e7962011-07-26 20:54:26 +00001087 case ARM::STR_PRE_IMM:
1088 case ARM::STR_PRE_REG:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001089 assert(MI->getOperand(2).getReg() == ARM::SP &&
1090 "Only stack pointer as a source reg is supported");
1091 RegList.push_back(SrcReg);
1092 break;
1093 }
1094 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1095 } else {
1096 // Changes of stack / frame pointer.
1097 if (SrcReg == ARM::SP) {
1098 int64_t Offset = 0;
1099 switch (Opc) {
1100 default:
1101 MI->dump();
1102 assert(0 && "Unsupported opcode for unwinding information");
1103 case ARM::MOVr:
1104 Offset = 0;
1105 break;
1106 case ARM::ADDri:
1107 Offset = -MI->getOperand(2).getImm();
1108 break;
1109 case ARM::SUBri:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001110 Offset = MI->getOperand(2).getImm();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001111 break;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001112 case ARM::tSUBspi:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001113 Offset = MI->getOperand(2).getImm()*4;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001114 break;
1115 case ARM::tADDspi:
1116 case ARM::tADDrSPi:
1117 Offset = -MI->getOperand(2).getImm()*4;
1118 break;
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001119 case ARM::tLDRpci: {
1120 // Grab the constpool index and check, whether it corresponds to
1121 // original or cloned constpool entry.
1122 unsigned CPI = MI->getOperand(1).getIndex();
1123 const MachineConstantPool *MCP = MF.getConstantPool();
1124 if (CPI >= MCP->getConstants().size())
1125 CPI = AFI.getOriginalCPIdx(CPI);
1126 assert(CPI != -1U && "Invalid constpool index");
1127
1128 // Derive the actual offset.
1129 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1130 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1131 // FIXME: Check for user, it should be "add" instruction!
1132 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001133 break;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001134 }
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001135 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001136
1137 if (DstReg == FramePtr && FramePtr != ARM::SP)
Anton Korobeynikove5163792011-03-05 18:44:00 +00001138 // Set-up of the frame pointer. Positive values correspond to "add"
1139 // instruction.
1140 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001141 else if (DstReg == ARM::SP) {
Anton Korobeynikove5163792011-03-05 18:44:00 +00001142 // Change of SP by an offset. Positive values correspond to "sub"
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001143 // instruction.
1144 OutStreamer.EmitPad(Offset);
1145 } else {
1146 MI->dump();
1147 assert(0 && "Unsupported opcode for unwinding information");
1148 }
1149 } else if (DstReg == ARM::SP) {
1150 // FIXME: .movsp goes here
1151 MI->dump();
1152 assert(0 && "Unsupported opcode for unwinding information");
1153 }
1154 else {
1155 MI->dump();
1156 assert(0 && "Unsupported opcode for unwinding information");
1157 }
1158 }
1159}
1160
1161extern cl::opt<bool> EnableARMEHABI;
1162
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001163// Simple pseudo-instructions have their lowering (with expansion to real
1164// instructions) auto-generated.
1165#include "ARMGenMCPseudoLowering.inc"
1166
Jim Grosbachb454cda2010-09-29 15:23:40 +00001167void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001168 // Emit unwinding stuff for frame-related instructions
1169 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1170 EmitUnwindingInstruction(MI);
1171
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001172 // Do any auto-generated pseudo lowerings.
1173 if (emitPseudoExpansionLowering(OutStreamer, MI))
1174 return;
1175
Andrew Trick3be654f2011-09-21 02:20:46 +00001176 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1177 "Pseudo flag setting opcode should be expanded early");
1178
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001179 // Check for manual lowerings.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001180 unsigned Opc = MI->getOpcode();
1181 switch (Opc) {
Chris Lattner112f2392010-11-14 20:31:06 +00001182 case ARM::t2MOVi32imm: assert(0 && "Should be lowered by thumb2it pass");
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001183 case ARM::DBG_VALUE: {
1184 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1185 SmallString<128> TmpStr;
1186 raw_svector_ostream OS(TmpStr);
1187 PrintDebugValueComment(MI, OS);
1188 OutStreamer.EmitRawText(StringRef(OS.str()));
1189 }
1190 return;
1191 }
Jim Grosbach40edf732010-12-14 21:10:47 +00001192 case ARM::LEApcrel:
Jim Grosbachd40963c2010-12-14 22:28:03 +00001193 case ARM::tLEApcrel:
Jim Grosbach40edf732010-12-14 21:10:47 +00001194 case ARM::t2LEApcrel: {
Jim Grosbachdff84b02010-12-02 00:28:45 +00001195 // FIXME: Need to also handle globals and externals
Jim Grosbachdff84b02010-12-02 00:28:45 +00001196 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001197 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1198 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1199 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001200 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1201 GetCPISymbol(MI->getOperand(1).getIndex()),
1202 MI->getOperand(2).getImm(), MI->getOperand(3).getReg(),
1203 OutContext);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001204 OutStreamer.EmitInstruction(TmpInst);
1205 return;
1206 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001207 case ARM::LEApcrelJT:
1208 case ARM::tLEApcrelJT:
1209 case ARM::t2LEApcrelJT: {
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001210 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001211 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1212 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1213 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001214 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1215 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1216 MI->getOperand(2).getImm()),
1217 MI->getOperand(3).getImm(), MI->getOperand(4).getReg(),
1218 OutContext);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001219 OutStreamer.EmitInstruction(TmpInst);
1220 return;
1221 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001222 // Darwin call instructions are just normal call instructions with different
1223 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001224 case ARM::BXr9_CALL:
1225 case ARM::BX_CALL: {
1226 {
1227 MCInst TmpInst;
1228 TmpInst.setOpcode(ARM::MOVr);
1229 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1230 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1231 // Add predicate operands.
1232 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1233 TmpInst.addOperand(MCOperand::CreateReg(0));
1234 // Add 's' bit operand (always reg0 for this)
1235 TmpInst.addOperand(MCOperand::CreateReg(0));
1236 OutStreamer.EmitInstruction(TmpInst);
1237 }
1238 {
1239 MCInst TmpInst;
1240 TmpInst.setOpcode(ARM::BX);
1241 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1242 OutStreamer.EmitInstruction(TmpInst);
1243 }
1244 return;
1245 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001246 case ARM::tBXr9_CALL:
1247 case ARM::tBX_CALL: {
1248 {
1249 MCInst TmpInst;
1250 TmpInst.setOpcode(ARM::tMOVr);
1251 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1252 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001253 // Add predicate operands.
1254 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1255 TmpInst.addOperand(MCOperand::CreateReg(0));
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001256 OutStreamer.EmitInstruction(TmpInst);
1257 }
1258 {
1259 MCInst TmpInst;
1260 TmpInst.setOpcode(ARM::tBX);
1261 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1262 // Add predicate operands.
1263 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1264 TmpInst.addOperand(MCOperand::CreateReg(0));
1265 OutStreamer.EmitInstruction(TmpInst);
1266 }
1267 return;
1268 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001269 case ARM::BMOVPCRXr9_CALL:
1270 case ARM::BMOVPCRX_CALL: {
1271 {
1272 MCInst TmpInst;
1273 TmpInst.setOpcode(ARM::MOVr);
1274 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1275 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1276 // Add predicate operands.
1277 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1278 TmpInst.addOperand(MCOperand::CreateReg(0));
1279 // Add 's' bit operand (always reg0 for this)
1280 TmpInst.addOperand(MCOperand::CreateReg(0));
1281 OutStreamer.EmitInstruction(TmpInst);
1282 }
1283 {
1284 MCInst TmpInst;
1285 TmpInst.setOpcode(ARM::MOVr);
1286 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1287 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1288 // Add predicate operands.
1289 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1290 TmpInst.addOperand(MCOperand::CreateReg(0));
1291 // Add 's' bit operand (always reg0 for this)
1292 TmpInst.addOperand(MCOperand::CreateReg(0));
1293 OutStreamer.EmitInstruction(TmpInst);
1294 }
1295 return;
1296 }
Evan Cheng53519f02011-01-21 18:55:51 +00001297 case ARM::MOVi16_ga_pcrel:
1298 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001299 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001300 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001301 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1302
Evan Cheng53519f02011-01-21 18:55:51 +00001303 unsigned TF = MI->getOperand(1).getTargetFlags();
1304 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001305 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1306 MCSymbol *GVSym = GetARMGVSymbol(GV);
1307 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001308 if (isPIC) {
1309 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1310 getFunctionNumber(),
1311 MI->getOperand(2).getImm(), OutContext);
1312 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1313 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1314 const MCExpr *PCRelExpr =
1315 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1316 MCBinaryExpr::CreateAdd(LabelSymExpr,
1317 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001318 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001319 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1320 } else {
1321 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1322 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1323 }
1324
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001325 // Add predicate operands.
1326 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1327 TmpInst.addOperand(MCOperand::CreateReg(0));
1328 // Add 's' bit operand (always reg0 for this)
1329 TmpInst.addOperand(MCOperand::CreateReg(0));
1330 OutStreamer.EmitInstruction(TmpInst);
1331 return;
1332 }
Evan Cheng53519f02011-01-21 18:55:51 +00001333 case ARM::MOVTi16_ga_pcrel:
1334 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001335 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001336 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1337 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001338 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1339 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1340
Evan Cheng53519f02011-01-21 18:55:51 +00001341 unsigned TF = MI->getOperand(2).getTargetFlags();
1342 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001343 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1344 MCSymbol *GVSym = GetARMGVSymbol(GV);
1345 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001346 if (isPIC) {
1347 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1348 getFunctionNumber(),
1349 MI->getOperand(3).getImm(), OutContext);
1350 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1351 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1352 const MCExpr *PCRelExpr =
1353 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1354 MCBinaryExpr::CreateAdd(LabelSymExpr,
1355 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001356 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001357 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1358 } else {
1359 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1360 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1361 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001362 // Add predicate operands.
1363 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1364 TmpInst.addOperand(MCOperand::CreateReg(0));
1365 // Add 's' bit operand (always reg0 for this)
1366 TmpInst.addOperand(MCOperand::CreateReg(0));
1367 OutStreamer.EmitInstruction(TmpInst);
1368 return;
1369 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001370 case ARM::tPICADD: {
1371 // This is a pseudo op for a label + instruction sequence, which looks like:
1372 // LPC0:
1373 // add r0, pc
1374 // This adds the address of LPC0 to r0.
1375
1376 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001377 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1378 getFunctionNumber(), MI->getOperand(2).getImm(),
1379 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001380
1381 // Form and emit the add.
1382 MCInst AddInst;
1383 AddInst.setOpcode(ARM::tADDhirr);
1384 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1385 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1386 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1387 // Add predicate operands.
1388 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1389 AddInst.addOperand(MCOperand::CreateReg(0));
1390 OutStreamer.EmitInstruction(AddInst);
1391 return;
1392 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001393 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001394 // This is a pseudo op for a label + instruction sequence, which looks like:
1395 // LPC0:
1396 // add r0, pc, r0
1397 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001398
Chris Lattner4d152222009-10-19 22:23:04 +00001399 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001400 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1401 getFunctionNumber(), MI->getOperand(2).getImm(),
1402 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001403
Jim Grosbachf3f09522010-09-14 21:05:34 +00001404 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +00001405 MCInst AddInst;
1406 AddInst.setOpcode(ARM::ADDrr);
1407 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1408 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1409 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +00001410 // Add predicate operands.
1411 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1412 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1413 // Add 's' bit operand (always reg0 for this)
1414 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +00001415 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001416 return;
1417 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001418 case ARM::PICSTR:
1419 case ARM::PICSTRB:
1420 case ARM::PICSTRH:
1421 case ARM::PICLDR:
1422 case ARM::PICLDRB:
1423 case ARM::PICLDRH:
1424 case ARM::PICLDRSB:
1425 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001426 // This is a pseudo op for a label + instruction sequence, which looks like:
1427 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001428 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001429 // The LCP0 label is referenced by a constant pool entry in order to get
1430 // a PC-relative address at the ldr instruction.
1431
1432 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001433 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1434 getFunctionNumber(), MI->getOperand(2).getImm(),
1435 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001436
1437 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001438 unsigned Opcode;
1439 switch (MI->getOpcode()) {
1440 default:
1441 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001442 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1443 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001444 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001445 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001446 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001447 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1448 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1449 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1450 }
1451 MCInst LdStInst;
1452 LdStInst.setOpcode(Opcode);
1453 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1454 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1455 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1456 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001457 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001458 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1459 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1460 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001461
1462 return;
1463 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001464 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001465 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1466 /// in the function. The first operand is the ID# for this instruction, the
1467 /// second is the index into the MachineConstantPool that this is, the third
1468 /// is the size in bytes of this constant pool entry.
1469 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1470 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1471
1472 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +00001473 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001474
1475 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1476 if (MCPE.isMachineConstantPoolEntry())
1477 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1478 else
1479 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +00001480
Chris Lattnera70e6442009-10-19 22:33:05 +00001481 return;
1482 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001483 case ARM::t2BR_JT: {
1484 // Lower and emit the instruction itself, then the jump table following it.
1485 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001486 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001487 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1488 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1489 // Add predicate operands.
1490 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1491 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001492 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001493 // Output the data for the jump table itself
1494 EmitJump2Table(MI);
1495 return;
1496 }
1497 case ARM::t2TBB_JT: {
1498 // Lower and emit the instruction itself, then the jump table following it.
1499 MCInst TmpInst;
1500
1501 TmpInst.setOpcode(ARM::t2TBB);
1502 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1503 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1504 // Add predicate operands.
1505 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1506 TmpInst.addOperand(MCOperand::CreateReg(0));
1507 OutStreamer.EmitInstruction(TmpInst);
1508 // Output the data for the jump table itself
1509 EmitJump2Table(MI);
1510 // Make sure the next instruction is 2-byte aligned.
1511 EmitAlignment(1);
1512 return;
1513 }
1514 case ARM::t2TBH_JT: {
1515 // Lower and emit the instruction itself, then the jump table following it.
1516 MCInst TmpInst;
1517
1518 TmpInst.setOpcode(ARM::t2TBH);
1519 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1520 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1521 // Add predicate operands.
1522 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1523 TmpInst.addOperand(MCOperand::CreateReg(0));
1524 OutStreamer.EmitInstruction(TmpInst);
1525 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001526 EmitJump2Table(MI);
1527 return;
1528 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001529 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001530 case ARM::BR_JTr: {
1531 // Lower and emit the instruction itself, then the jump table following it.
1532 // mov pc, target
1533 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001534 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001535 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001536 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001537 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1538 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1539 // Add predicate operands.
1540 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1541 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001542 // Add 's' bit operand (always reg0 for this)
1543 if (Opc == ARM::MOVr)
1544 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001545 OutStreamer.EmitInstruction(TmpInst);
1546
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001547 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001548 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001549 EmitAlignment(2);
1550
Jim Grosbach2dc77682010-11-29 18:37:44 +00001551 // Output the data for the jump table itself
1552 EmitJumpTable(MI);
1553 return;
1554 }
1555 case ARM::BR_JTm: {
1556 // Lower and emit the instruction itself, then the jump table following it.
1557 // ldr pc, target
1558 MCInst TmpInst;
1559 if (MI->getOperand(1).getReg() == 0) {
1560 // literal offset
1561 TmpInst.setOpcode(ARM::LDRi12);
1562 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1563 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1564 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1565 } else {
1566 TmpInst.setOpcode(ARM::LDRrs);
1567 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1568 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1569 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1570 TmpInst.addOperand(MCOperand::CreateImm(0));
1571 }
1572 // Add predicate operands.
1573 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1574 TmpInst.addOperand(MCOperand::CreateReg(0));
1575 OutStreamer.EmitInstruction(TmpInst);
1576
1577 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001578 EmitJumpTable(MI);
1579 return;
1580 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001581 case ARM::BR_JTadd: {
1582 // Lower and emit the instruction itself, then the jump table following it.
1583 // add pc, target, idx
Jim Grosbach2dc77682010-11-29 18:37:44 +00001584 MCInst TmpInst;
1585 TmpInst.setOpcode(ARM::ADDrr);
1586 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1587 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1588 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001589 // Add predicate operands.
Jim Grosbach2dc77682010-11-29 18:37:44 +00001590 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1591 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001592 // Add 's' bit operand (always reg0 for this)
Jim Grosbach2dc77682010-11-29 18:37:44 +00001593 TmpInst.addOperand(MCOperand::CreateReg(0));
1594 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001595
1596 // Output the data for the jump table itself
1597 EmitJumpTable(MI);
1598 return;
1599 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001600 case ARM::TRAP: {
1601 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1602 // FIXME: Remove this special case when they do.
1603 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001604 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001605 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001606 OutStreamer.AddComment("trap");
1607 OutStreamer.EmitIntValue(Val, 4);
1608 return;
1609 }
1610 break;
1611 }
1612 case ARM::tTRAP: {
1613 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1614 // FIXME: Remove this special case when they do.
1615 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001616 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001617 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001618 OutStreamer.AddComment("trap");
1619 OutStreamer.EmitIntValue(Val, 2);
1620 return;
1621 }
1622 break;
1623 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001624 case ARM::t2Int_eh_sjlj_setjmp:
1625 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001626 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001627 // Two incoming args: GPR:$src, GPR:$val
1628 // mov $val, pc
1629 // adds $val, #7
1630 // str $val, [$src, #4]
1631 // movs r0, #0
1632 // b 1f
1633 // movs r0, #1
1634 // 1:
1635 unsigned SrcReg = MI->getOperand(0).getReg();
1636 unsigned ValReg = MI->getOperand(1).getReg();
1637 MCSymbol *Label = GetARMSJLJEHLabel();
1638 {
1639 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001640 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach433a5782010-09-24 20:47:58 +00001641 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1642 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001643 // Predicate.
1644 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1645 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001646 OutStreamer.AddComment("eh_setjmp begin");
1647 OutStreamer.EmitInstruction(TmpInst);
1648 }
1649 {
1650 MCInst TmpInst;
1651 TmpInst.setOpcode(ARM::tADDi3);
1652 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1653 // 's' bit operand
1654 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1655 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1656 TmpInst.addOperand(MCOperand::CreateImm(7));
1657 // Predicate.
1658 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1659 TmpInst.addOperand(MCOperand::CreateReg(0));
1660 OutStreamer.EmitInstruction(TmpInst);
1661 }
1662 {
1663 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001664 TmpInst.setOpcode(ARM::tSTRi);
Jim Grosbach433a5782010-09-24 20:47:58 +00001665 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1666 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1667 // The offset immediate is #4. The operand value is scaled by 4 for the
1668 // tSTR instruction.
1669 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach433a5782010-09-24 20:47:58 +00001670 // Predicate.
1671 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1672 TmpInst.addOperand(MCOperand::CreateReg(0));
1673 OutStreamer.EmitInstruction(TmpInst);
1674 }
1675 {
1676 MCInst TmpInst;
1677 TmpInst.setOpcode(ARM::tMOVi8);
1678 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1679 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1680 TmpInst.addOperand(MCOperand::CreateImm(0));
1681 // Predicate.
1682 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1683 TmpInst.addOperand(MCOperand::CreateReg(0));
1684 OutStreamer.EmitInstruction(TmpInst);
1685 }
1686 {
1687 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1688 MCInst TmpInst;
1689 TmpInst.setOpcode(ARM::tB);
1690 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001691 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1692 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001693 OutStreamer.EmitInstruction(TmpInst);
1694 }
1695 {
1696 MCInst TmpInst;
1697 TmpInst.setOpcode(ARM::tMOVi8);
1698 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1699 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1700 TmpInst.addOperand(MCOperand::CreateImm(1));
1701 // Predicate.
1702 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1703 TmpInst.addOperand(MCOperand::CreateReg(0));
1704 OutStreamer.AddComment("eh_setjmp end");
1705 OutStreamer.EmitInstruction(TmpInst);
1706 }
1707 OutStreamer.EmitLabel(Label);
1708 return;
1709 }
1710
Jim Grosbach45390082010-09-23 23:33:56 +00001711 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001712 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001713 // Two incoming args: GPR:$src, GPR:$val
1714 // add $val, pc, #8
1715 // str $val, [$src, #+4]
1716 // mov r0, #0
1717 // add pc, pc, #0
1718 // mov r0, #1
1719 unsigned SrcReg = MI->getOperand(0).getReg();
1720 unsigned ValReg = MI->getOperand(1).getReg();
1721
1722 {
1723 MCInst TmpInst;
1724 TmpInst.setOpcode(ARM::ADDri);
1725 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1726 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1727 TmpInst.addOperand(MCOperand::CreateImm(8));
1728 // Predicate.
1729 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1730 TmpInst.addOperand(MCOperand::CreateReg(0));
1731 // 's' bit operand (always reg0 for this).
1732 TmpInst.addOperand(MCOperand::CreateReg(0));
1733 OutStreamer.AddComment("eh_setjmp begin");
1734 OutStreamer.EmitInstruction(TmpInst);
1735 }
1736 {
1737 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001738 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001739 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1740 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001741 TmpInst.addOperand(MCOperand::CreateImm(4));
1742 // Predicate.
1743 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1744 TmpInst.addOperand(MCOperand::CreateReg(0));
1745 OutStreamer.EmitInstruction(TmpInst);
1746 }
1747 {
1748 MCInst TmpInst;
1749 TmpInst.setOpcode(ARM::MOVi);
1750 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1751 TmpInst.addOperand(MCOperand::CreateImm(0));
1752 // Predicate.
1753 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1754 TmpInst.addOperand(MCOperand::CreateReg(0));
1755 // 's' bit operand (always reg0 for this).
1756 TmpInst.addOperand(MCOperand::CreateReg(0));
1757 OutStreamer.EmitInstruction(TmpInst);
1758 }
1759 {
1760 MCInst TmpInst;
1761 TmpInst.setOpcode(ARM::ADDri);
1762 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1763 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1764 TmpInst.addOperand(MCOperand::CreateImm(0));
1765 // Predicate.
1766 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1767 TmpInst.addOperand(MCOperand::CreateReg(0));
1768 // 's' bit operand (always reg0 for this).
1769 TmpInst.addOperand(MCOperand::CreateReg(0));
1770 OutStreamer.EmitInstruction(TmpInst);
1771 }
1772 {
1773 MCInst TmpInst;
1774 TmpInst.setOpcode(ARM::MOVi);
1775 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1776 TmpInst.addOperand(MCOperand::CreateImm(1));
1777 // Predicate.
1778 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1779 TmpInst.addOperand(MCOperand::CreateReg(0));
1780 // 's' bit operand (always reg0 for this).
1781 TmpInst.addOperand(MCOperand::CreateReg(0));
1782 OutStreamer.AddComment("eh_setjmp end");
1783 OutStreamer.EmitInstruction(TmpInst);
1784 }
1785 return;
1786 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001787 case ARM::Int_eh_sjlj_longjmp: {
1788 // ldr sp, [$src, #8]
1789 // ldr $scratch, [$src, #4]
1790 // ldr r7, [$src]
1791 // bx $scratch
1792 unsigned SrcReg = MI->getOperand(0).getReg();
1793 unsigned ScratchReg = MI->getOperand(1).getReg();
1794 {
1795 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001796 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001797 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1798 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001799 TmpInst.addOperand(MCOperand::CreateImm(8));
1800 // Predicate.
1801 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1802 TmpInst.addOperand(MCOperand::CreateReg(0));
1803 OutStreamer.EmitInstruction(TmpInst);
1804 }
1805 {
1806 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001807 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001808 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1809 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001810 TmpInst.addOperand(MCOperand::CreateImm(4));
1811 // Predicate.
1812 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1813 TmpInst.addOperand(MCOperand::CreateReg(0));
1814 OutStreamer.EmitInstruction(TmpInst);
1815 }
1816 {
1817 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001818 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001819 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1820 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001821 TmpInst.addOperand(MCOperand::CreateImm(0));
1822 // Predicate.
1823 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1824 TmpInst.addOperand(MCOperand::CreateReg(0));
1825 OutStreamer.EmitInstruction(TmpInst);
1826 }
1827 {
1828 MCInst TmpInst;
Bill Wendling6e46d842010-11-30 00:48:15 +00001829 TmpInst.setOpcode(ARM::BX);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001830 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1831 // Predicate.
1832 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1833 TmpInst.addOperand(MCOperand::CreateReg(0));
1834 OutStreamer.EmitInstruction(TmpInst);
1835 }
1836 return;
1837 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001838 case ARM::tInt_eh_sjlj_longjmp: {
1839 // ldr $scratch, [$src, #8]
1840 // mov sp, $scratch
1841 // ldr $scratch, [$src, #4]
1842 // ldr r7, [$src]
1843 // bx $scratch
1844 unsigned SrcReg = MI->getOperand(0).getReg();
1845 unsigned ScratchReg = MI->getOperand(1).getReg();
1846 {
1847 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001848 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001849 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1850 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1851 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001852 // tLDR instruction.
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001853 TmpInst.addOperand(MCOperand::CreateImm(2));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001854 // Predicate.
1855 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1856 TmpInst.addOperand(MCOperand::CreateReg(0));
1857 OutStreamer.EmitInstruction(TmpInst);
1858 }
1859 {
1860 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001861 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001862 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1863 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1864 // Predicate.
1865 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1866 TmpInst.addOperand(MCOperand::CreateReg(0));
1867 OutStreamer.EmitInstruction(TmpInst);
1868 }
1869 {
1870 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001871 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001872 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1873 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1874 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001875 // Predicate.
1876 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1877 TmpInst.addOperand(MCOperand::CreateReg(0));
1878 OutStreamer.EmitInstruction(TmpInst);
1879 }
1880 {
1881 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001882 TmpInst.setOpcode(ARM::tLDRr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001883 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1884 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001885 TmpInst.addOperand(MCOperand::CreateReg(0));
1886 // Predicate.
1887 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1888 TmpInst.addOperand(MCOperand::CreateReg(0));
1889 OutStreamer.EmitInstruction(TmpInst);
1890 }
1891 {
1892 MCInst TmpInst;
Cameron Zwarich421b1062011-05-26 03:41:12 +00001893 TmpInst.setOpcode(ARM::tBX);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001894 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1895 // Predicate.
1896 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1897 TmpInst.addOperand(MCOperand::CreateReg(0));
1898 OutStreamer.EmitInstruction(TmpInst);
1899 }
1900 return;
1901 }
Chris Lattner97f06932009-10-19 20:20:46 +00001902 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001903
Chris Lattner97f06932009-10-19 20:20:46 +00001904 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00001905 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001906
Chris Lattner850d2e22010-02-03 01:16:28 +00001907 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001908}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001909
1910//===----------------------------------------------------------------------===//
1911// Target Registry Stuff
1912//===----------------------------------------------------------------------===//
1913
Daniel Dunbar2685a292009-10-20 05:15:36 +00001914// Force static initialization.
1915extern "C" void LLVMInitializeARMAsmPrinter() {
1916 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1917 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001918}
1919