blob: 7951cfabb8b24b452b2c2683c19e081a98b7060e [file] [log] [blame]
Chris Lattner97f06932009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Anton Korobeynikov88ce6672009-05-23 19:51:20 +000017#include "ARMBuildAttrs.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
Jim Grosbach7ac16092010-10-01 22:39:28 +000020#include "InstPrinter/ARMInstPrinter.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
22#include "ARMMCInstLower.h"
23#include "ARMTargetMachine.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000024#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000025#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000027#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
35#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000036#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000037#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000041#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000042#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000043#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000044#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000045#include "llvm/Target/TargetRegistry.h"
Evan Chengc324ecb2009-07-24 18:19:46 +000046#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000047#include "llvm/ADT/SmallString.h"
Bob Wilson54c78ef2009-11-06 23:33:28 +000048#include "llvm/ADT/StringExtras.h"
Chris Lattner97f06932009-10-19 20:20:46 +000049#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000050#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000051#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000053#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000054using namespace llvm;
55
Jim Grosbach91729002010-07-21 23:03:52 +000056namespace llvm {
57 namespace ARM {
58 enum DW_ISA {
59 DW_ISA_ARM_thumb = 1,
60 DW_ISA_ARM_arm = 2
61 };
62 }
63}
64
Chris Lattner95b2c7d2006-12-19 22:59:26 +000065namespace {
Chris Lattner4a071d62009-10-19 17:59:19 +000066 class ARMAsmPrinter : public AsmPrinter {
Evan Chenga8e29892007-01-19 07:51:42 +000067
68 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
69 /// make the right decision when printing asm code for different targets.
70 const ARMSubtarget *Subtarget;
71
72 /// AFI - Keep a pointer to ARMFunctionInfo for the current
Evan Cheng6d63a722008-09-18 07:27:23 +000073 /// MachineFunction.
Evan Chenga8e29892007-01-19 07:51:42 +000074 ARMFunctionInfo *AFI;
75
Evan Cheng6d63a722008-09-18 07:27:23 +000076 /// MCP - Keep a pointer to constantpool entries of the current
77 /// MachineFunction.
78 const MachineConstantPool *MCP;
79
Bill Wendling57f0db82009-02-24 08:30:20 +000080 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000081 explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
82 : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
Bill Wendling57f0db82009-02-24 08:30:20 +000083 Subtarget = &TM.getSubtarget<ARMSubtarget>();
84 }
85
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000086 virtual const char *getPassName() const {
87 return "ARM Assembly Printer";
88 }
Jim Grosbachb0739b72010-09-02 01:02:06 +000089
Chris Lattner35c33bd2010-04-04 04:47:45 +000090 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +000091 const char *Modifier = 0);
Bob Wilson54c78ef2009-11-06 23:33:28 +000092
Evan Cheng055b0312009-06-29 07:51:04 +000093 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +000094 unsigned AsmVariant, const char *ExtraCode,
95 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +000096 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
Bob Wilson224c2442009-05-19 05:53:42 +000097 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +000098 const char *ExtraCode, raw_ostream &O);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000099
Jim Grosbach2317e402010-09-30 01:57:53 +0000100 void EmitJumpTable(const MachineInstr *MI);
101 void EmitJump2Table(const MachineInstr *MI);
Chris Lattnera786cea2010-01-28 01:10:34 +0000102 virtual void EmitInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000103 bool runOnMachineFunction(MachineFunction &F);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000104
Chris Lattnera2406192010-01-28 00:19:24 +0000105 virtual void EmitConstantPool() {} // we emit constant pools customly!
Chris Lattner953ebb72010-01-27 23:58:11 +0000106 virtual void EmitFunctionEntryLabel();
Bob Wilson812209a2009-09-30 22:06:26 +0000107 void EmitStartOfAsmFile(Module &M);
Chris Lattner4a071d62009-10-19 17:59:19 +0000108 void EmitEndOfAsmFile(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000109
Jason W Kimdef9ac42010-10-06 22:36:46 +0000110 private:
111 // Helpers for EmitStartOfAsmFile() and EmitEndOfAsmFile()
112 void emitAttributes();
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000113 void emitAttribute(ARMBuildAttrs::AttrType attr, int v);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000114
115 public:
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000116 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
117
Devang Patel59135f42010-08-04 22:39:39 +0000118 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
119 MachineLocation Location;
120 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
121 // Frame address. Currently handles register +- offset only.
122 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
123 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
124 else {
125 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
126 }
127 return Location;
128 }
129
Jim Grosbach91729002010-07-21 23:03:52 +0000130 virtual unsigned getISAEncoding() {
131 // ARM/Darwin adds ISA to the DWARF info for each function.
132 if (!Subtarget->isTargetDarwin())
133 return 0;
134 return Subtarget->isThumb() ?
135 llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
136 }
137
Chris Lattner0890cf12010-01-25 19:51:38 +0000138 MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
139 const MachineBasicBlock *MBB) const;
140 MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000141
Jim Grosbach433a5782010-09-24 20:47:58 +0000142 MCSymbol *GetARMSJLJEHLabel(void) const;
143
Evan Cheng711b6dc2008-08-08 06:56:16 +0000144 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
145 /// the .s file.
Evan Chenga8e29892007-01-19 07:51:42 +0000146 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000147 SmallString<128> Str;
148 raw_svector_ostream OS(Str);
149 EmitMachineConstantPoolValue(MCPV, OS);
150 OutStreamer.EmitRawText(OS.str());
151 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000152
Chris Lattner9d7efd32010-04-04 07:05:53 +0000153 void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
154 raw_ostream &O) {
Chris Lattnerea3cb402010-01-20 07:33:29 +0000155 switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
156 case 1: O << MAI->getData8bitsDirective(0); break;
157 case 2: O << MAI->getData16bitsDirective(0); break;
158 case 4: O << MAI->getData32bitsDirective(0); break;
159 default: assert(0 && "Unknown CPV size");
160 }
Evan Chenga8e29892007-01-19 07:51:42 +0000161
Evan Cheng711b6dc2008-08-08 06:56:16 +0000162 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000163
164 if (ACPV->isLSDA()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000165 O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Bob Wilson28989a82009-11-02 16:59:06 +0000166 } else if (ACPV->isBlockAddress()) {
Chris Lattner0752cda2010-04-05 16:32:14 +0000167 O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
Bob Wilson28989a82009-11-02 16:59:06 +0000168 } else if (ACPV->isGlobalValue()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000169 const GlobalValue *GV = ACPV->getGV();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000170 bool isIndirect = Subtarget->isTargetDarwin() &&
Evan Cheng63476a82009-09-03 07:04:02 +0000171 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000172 if (!isIndirect)
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000173 O << *Mang->getSymbol(GV);
Evan Chenge4e4ed32009-08-28 23:18:09 +0000174 else {
175 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Chris Lattner7a2ba942010-01-16 18:37:32 +0000176 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner10b318b2010-01-17 21:43:43 +0000177 O << *Sym;
Jim Grosbachb0739b72010-09-02 01:02:06 +0000178
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000179 MachineModuleInfoMachO &MMIMachO =
180 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Bill Wendlingcebae362010-03-10 22:34:10 +0000181 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000182 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
183 MMIMachO.getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000184 if (StubSym.getPointer() == 0)
185 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000186 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000187 }
Bob Wilson28989a82009-11-02 16:59:06 +0000188 } else {
189 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Chris Lattner10b318b2010-01-17 21:43:43 +0000190 O << *GetExternalSymbolSymbol(ACPV->getSymbol());
Bob Wilson28989a82009-11-02 16:59:06 +0000191 }
Jim Grosbache9952212009-09-04 01:38:51 +0000192
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000193 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000194 if (ACPV->getPCAdjustment() != 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000195 O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
Evan Chenge7e0d622009-11-06 22:24:13 +0000196 << getFunctionNumber() << "_" << ACPV->getLabelId()
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000197 << "+" << (unsigned)ACPV->getPCAdjustment();
198 if (ACPV->mustAddCurrentAddress())
199 O << "-.";
Chris Lattner8b378752010-01-15 23:26:49 +0000200 O << ')';
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000201 }
Evan Chenga8e29892007-01-19 07:51:42 +0000202 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000203 };
204} // end of anonymous namespace
205
Chris Lattner953ebb72010-01-27 23:58:11 +0000206void ARMAsmPrinter::EmitFunctionEntryLabel() {
207 if (AFI->isThumbFunction()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000208 OutStreamer.EmitRawText(StringRef("\t.code\t16"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000209 if (!Subtarget->isTargetDarwin())
Chris Lattner9d7efd32010-04-04 07:05:53 +0000210 OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000211 else {
212 // This needs to emit to a temporary string to get properly quoted
213 // MCSymbols when they have spaces in them.
214 SmallString<128> Tmp;
215 raw_svector_ostream OS(Tmp);
216 OS << "\t.thumb_func\t" << *CurrentFnSym;
217 OutStreamer.EmitRawText(OS.str());
218 }
Chris Lattner953ebb72010-01-27 23:58:11 +0000219 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000220
Chris Lattner953ebb72010-01-27 23:58:11 +0000221 OutStreamer.EmitLabel(CurrentFnSym);
222}
223
Jim Grosbach2317e402010-09-30 01:57:53 +0000224/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000225/// method to print assembly for each instruction.
226///
227bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000228 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000229 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000230
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000231 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000232}
233
Evan Cheng055b0312009-06-29 07:51:04 +0000234void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000235 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000236 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000237 unsigned TF = MO.getTargetFlags();
238
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000239 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000240 default:
241 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000242 case MachineOperand::MO_Register: {
243 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000244 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000245 assert(!MO.getSubReg() && "Subregs should be eliminated!");
246 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000247 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000248 }
Evan Chenga8e29892007-01-19 07:51:42 +0000249 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000250 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000251 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000252 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000253 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000254 O << ":lower16:";
255 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jim Grosbach4dea9412010-10-06 16:51:55 +0000256 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000257 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000258 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000259 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000260 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000261 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000262 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000263 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000264 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000265 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000266 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
267 (TF & ARMII::MO_LO16))
268 O << ":lower16:";
269 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
270 (TF & ARMII::MO_HI16))
271 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000272 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000273
Chris Lattner0c08d092010-04-03 22:28:33 +0000274 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000275 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000276 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000277 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000278 }
Evan Chenga8e29892007-01-19 07:51:42 +0000279 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000280 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000281 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000282 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000283 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000284 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000285 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000286 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000287 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000288 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000289 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000290 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000291 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000292}
293
Evan Cheng055b0312009-06-29 07:51:04 +0000294//===--------------------------------------------------------------------===//
295
Chris Lattner0890cf12010-01-25 19:51:38 +0000296MCSymbol *ARMAsmPrinter::
297GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
298 const MachineBasicBlock *MBB) const {
299 SmallString<60> Name;
300 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000301 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000302 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000303 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000304}
305
306MCSymbol *ARMAsmPrinter::
307GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
308 SmallString<60> Name;
309 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000310 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000311 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000312}
313
Jim Grosbach433a5782010-09-24 20:47:58 +0000314
315MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
316 SmallString<60> Name;
317 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
318 << getFunctionNumber();
319 return OutContext.GetOrCreateSymbol(Name.str());
320}
321
Evan Cheng055b0312009-06-29 07:51:04 +0000322bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000323 unsigned AsmVariant, const char *ExtraCode,
324 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000325 // Does this asm operand have a single letter operand modifier?
326 if (ExtraCode && ExtraCode[0]) {
327 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000328
Evan Chenga8e29892007-01-19 07:51:42 +0000329 switch (ExtraCode[0]) {
330 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000331 case 'a': // Print as a memory address.
332 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000333 O << "["
334 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
335 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000336 return false;
337 }
338 // Fallthrough
339 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000340 if (!MI->getOperand(OpNum).isImm())
341 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000342 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000343 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000344 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000345 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000346 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000347 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000348 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +0000349 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +0000350 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +0000351 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +0000352 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000353 }
Evan Chenga8e29892007-01-19 07:51:42 +0000354 }
Jim Grosbache9952212009-09-04 01:38:51 +0000355
Chris Lattner35c33bd2010-04-04 04:47:45 +0000356 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000357 return false;
358}
359
Bob Wilson224c2442009-05-19 05:53:42 +0000360bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000361 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000362 const char *ExtraCode,
363 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +0000364 if (ExtraCode && ExtraCode[0])
365 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +0000366
367 const MachineOperand &MO = MI->getOperand(OpNum);
368 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000369 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000370 return false;
371}
372
Bob Wilson812209a2009-09-30 22:06:26 +0000373void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000374 if (Subtarget->isTargetDarwin()) {
375 Reloc::Model RelocM = TM.getRelocationModel();
376 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
377 // Declare all the text sections up front (before the DWARF sections
378 // emitted by AsmPrinter::doInitialization) so the assembler will keep
379 // them together at the beginning of the object file. This helps
380 // avoid out-of-range branches that are due a fundamental limitation of
381 // the way symbol offsets are encoded with the current Darwin ARM
382 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000383 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000384 static_cast<const TargetLoweringObjectFileMachO &>(
385 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000386 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
387 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
388 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
389 if (RelocM == Reloc::DynamicNoPIC) {
390 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000391 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
392 MCSectionMachO::S_SYMBOL_STUBS,
393 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000394 OutStreamer.SwitchSection(sect);
395 } else {
396 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000397 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
398 MCSectionMachO::S_SYMBOL_STUBS,
399 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000400 OutStreamer.SwitchSection(sect);
401 }
Bob Wilson63db5942010-07-30 19:55:47 +0000402 const MCSection *StaticInitSect =
403 OutContext.getMachOSection("__TEXT", "__StaticInit",
404 MCSectionMachO::S_REGULAR |
405 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
406 SectionKind::getText());
407 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000408 }
409 }
410
Jim Grosbache5165492009-11-09 00:11:35 +0000411 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000412 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000413
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000414 // Emit ARM Build Attributes
415 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000416
Jason W Kimdef9ac42010-10-06 22:36:46 +0000417 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000418 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000419}
420
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000421
Chris Lattner4a071d62009-10-19 17:59:19 +0000422void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000423 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000424 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000425 const TargetLoweringObjectFileMachO &TLOFMacho =
426 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000427 MachineModuleInfoMachO &MMIMacho =
428 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000429
Evan Chenga8e29892007-01-19 07:51:42 +0000430 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000431 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000432
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000433 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000434 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000435 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000436 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000437 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000438 // L_foo$stub:
439 OutStreamer.EmitLabel(Stubs[i].first);
440 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000441 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
442 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000443
Bill Wendling52a50e52010-03-11 01:18:13 +0000444 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000445 // External to current translation unit.
446 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
447 else
448 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000449 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000450 // When we place the LSDA into the TEXT section, the type info
451 // pointers need to be indirect and pc-rel. We accomplish this by
452 // using NLPs; however, sometimes the types are local to the file.
453 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000454 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
455 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000456 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000457 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000458
459 Stubs.clear();
460 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000461 }
462
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000463 Stubs = MMIMacho.GetHiddenGVStubList();
464 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000465 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000466 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000467 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
468 // L_foo$stub:
469 OutStreamer.EmitLabel(Stubs[i].first);
470 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000471 OutStreamer.EmitValue(MCSymbolRefExpr::
472 Create(Stubs[i].second.getPointer(),
473 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000474 4/*size*/, 0/*addrspace*/);
475 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000476
477 Stubs.clear();
478 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000479 }
480
Evan Chenga8e29892007-01-19 07:51:42 +0000481 // Funny Darwin hack: This flag tells the linker that no global symbols
482 // contain code that falls through to other global symbols (e.g. the obvious
483 // implementation of multiple entry points). If this doesn't occur, the
484 // linker can safely perform dead code stripping. Since LLVM never
485 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000486 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000487 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000488}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000489
Chris Lattner97f06932009-10-19 20:20:46 +0000490//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000491// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
492// FIXME:
493// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000494// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000495// Instead of subclassing the MCELFStreamer, we do the work here.
496
497void ARMAsmPrinter::emitAttributes() {
498 // FIXME: Add in ELF specific section handling here.
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000499
Jason W Kimdef9ac42010-10-06 22:36:46 +0000500 // FIXME: unify this: .cpu and CPUString with enum attributes
501 std::string CPUString = Subtarget->getCPUString();
502 if (CPUString != "generic")
503 OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
504
505 // FIXME: Emit FPU type
506 if (Subtarget->hasVFP2())
507 emitAttribute(ARMBuildAttrs::VFP_arch, 2);
508
509 // Signal various FP modes.
510 if (!UnsafeFPMath) {
511 emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 1);
512 emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 1);
513 }
514
515 if (NoInfsFPMath && NoNaNsFPMath)
516 emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 1);
517 else
518 emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 3);
519
520 // 8-bytes alignment stuff.
521 emitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
522 emitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
523
524 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
525 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
526 emitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
527 emitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
528 }
529 // FIXME: Should we signal R9 usage?
530}
531
532void ARMAsmPrinter::emitAttribute(ARMBuildAttrs::AttrType attr, int v) {
533 if (OutStreamer.hasRawTextSupport()) {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000534 OutStreamer.EmitRawText("\t.eabi_attribute " +
Jason W Kimdef9ac42010-10-06 22:36:46 +0000535 Twine(attr) + ", " + Twine(v));
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000536
Jason W Kimdef9ac42010-10-06 22:36:46 +0000537 } else {
538 assert(0 && "ELF .ARM.attributes unimplemented");
539 }
540}
541
542//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000543
Jim Grosbach988ce092010-09-18 00:05:05 +0000544static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
545 unsigned LabelId, MCContext &Ctx) {
546
547 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
548 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
549 return Label;
550}
551
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000552void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
553 unsigned Opcode = MI->getOpcode();
554 int OpNum = 1;
555 if (Opcode == ARM::BR_JTadd)
556 OpNum = 2;
557 else if (Opcode == ARM::BR_JTm)
558 OpNum = 3;
559
560 const MachineOperand &MO1 = MI->getOperand(OpNum);
561 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
562 unsigned JTI = MO1.getIndex();
563
564 // Emit a label for the jump table.
565 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
566 OutStreamer.EmitLabel(JTISymbol);
567
568 // Emit each entry of the table.
569 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
570 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
571 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
572
573 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
574 MachineBasicBlock *MBB = JTBBs[i];
575 // Construct an MCExpr for the entry. We want a value of the form:
576 // (BasicBlockAddr - TableBeginAddr)
577 //
578 // For example, a table with entries jumping to basic blocks BB0 and BB1
579 // would look like:
580 // LJTI_0_0:
581 // .word (LBB0 - LJTI_0_0)
582 // .word (LBB1 - LJTI_0_0)
583 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
584
585 if (TM.getRelocationModel() == Reloc::PIC_)
586 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
587 OutContext),
588 OutContext);
589 OutStreamer.EmitValue(Expr, 4);
590 }
591}
592
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000593void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
594 unsigned Opcode = MI->getOpcode();
595 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
596 const MachineOperand &MO1 = MI->getOperand(OpNum);
597 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
598 unsigned JTI = MO1.getIndex();
599
600 // Emit a label for the jump table.
601 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
602 OutStreamer.EmitLabel(JTISymbol);
603
604 // Emit each entry of the table.
605 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
606 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
607 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000608 unsigned OffsetWidth = 4;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000609 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000610 OffsetWidth = 1;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000611 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000612 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000613
614 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
615 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000616 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
617 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000618 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000619 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000620 MCInst BrInst;
621 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000622 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000623 OutStreamer.EmitInstruction(BrInst);
624 continue;
625 }
626 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000627 // MCExpr for the entry. We want a value of the form:
628 // (BasicBlockAddr - TableBeginAddr) / 2
629 //
630 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
631 // would look like:
632 // LJTI_0_0:
633 // .byte (LBB0 - LJTI_0_0) / 2
634 // .byte (LBB1 - LJTI_0_0) / 2
635 const MCExpr *Expr =
636 MCBinaryExpr::CreateSub(MBBSymbolExpr,
637 MCSymbolRefExpr::Create(JTISymbol, OutContext),
638 OutContext);
639 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
640 OutContext);
641 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000642 }
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000643
644 // Make sure the instruction that follows TBB is 2-byte aligned.
645 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
646 if (MI->getOpcode() == ARM::t2TBB)
647 EmitAlignment(1);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000648}
649
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000650void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
651 raw_ostream &OS) {
652 unsigned NOps = MI->getNumOperands();
653 assert(NOps==4);
654 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
655 // cast away const; DIetc do not take const operands for some reason.
656 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
657 OS << V.getName();
658 OS << " <- ";
659 // Frame address. Currently handles register +- offset only.
660 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
661 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
662 OS << ']';
663 OS << "+";
664 printOperand(MI, NOps-2, OS);
665}
666
Jim Grosbachb454cda2010-09-29 15:23:40 +0000667void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +0000668 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +0000669 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +0000670 case ARM::t2MOVi32imm:
671 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +0000672 default: break;
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000673 case ARM::DBG_VALUE: {
674 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
675 SmallString<128> TmpStr;
676 raw_svector_ostream OS(TmpStr);
677 PrintDebugValueComment(MI, OS);
678 OutStreamer.EmitRawText(StringRef(OS.str()));
679 }
680 return;
681 }
Jim Grosbachfbd18732010-09-17 23:41:53 +0000682 case ARM::tPICADD: {
683 // This is a pseudo op for a label + instruction sequence, which looks like:
684 // LPC0:
685 // add r0, pc
686 // This adds the address of LPC0 to r0.
687
688 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000689 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
690 getFunctionNumber(), MI->getOperand(2).getImm(),
691 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +0000692
693 // Form and emit the add.
694 MCInst AddInst;
695 AddInst.setOpcode(ARM::tADDhirr);
696 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
697 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
698 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
699 // Add predicate operands.
700 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
701 AddInst.addOperand(MCOperand::CreateReg(0));
702 OutStreamer.EmitInstruction(AddInst);
703 return;
704 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000705 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +0000706 // This is a pseudo op for a label + instruction sequence, which looks like:
707 // LPC0:
708 // add r0, pc, r0
709 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000710
Chris Lattner4d152222009-10-19 22:23:04 +0000711 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000712 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
713 getFunctionNumber(), MI->getOperand(2).getImm(),
714 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000715
Jim Grosbachf3f09522010-09-14 21:05:34 +0000716 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +0000717 MCInst AddInst;
718 AddInst.setOpcode(ARM::ADDrr);
719 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
720 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
721 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +0000722 // Add predicate operands.
723 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
724 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
725 // Add 's' bit operand (always reg0 for this)
726 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +0000727 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +0000728 return;
729 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000730 case ARM::PICSTR:
731 case ARM::PICSTRB:
732 case ARM::PICSTRH:
733 case ARM::PICLDR:
734 case ARM::PICLDRB:
735 case ARM::PICLDRH:
736 case ARM::PICLDRSB:
737 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000738 // This is a pseudo op for a label + instruction sequence, which looks like:
739 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000740 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000741 // The LCP0 label is referenced by a constant pool entry in order to get
742 // a PC-relative address at the ldr instruction.
743
744 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +0000745 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
746 getFunctionNumber(), MI->getOperand(2).getImm(),
747 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000748
749 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000750 unsigned Opcode;
751 switch (MI->getOpcode()) {
752 default:
753 llvm_unreachable("Unexpected opcode!");
754 case ARM::PICSTR: Opcode = ARM::STR; break;
755 case ARM::PICSTRB: Opcode = ARM::STRB; break;
756 case ARM::PICSTRH: Opcode = ARM::STRH; break;
757 case ARM::PICLDR: Opcode = ARM::LDR; break;
758 case ARM::PICLDRB: Opcode = ARM::LDRB; break;
759 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
760 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
761 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
762 }
763 MCInst LdStInst;
764 LdStInst.setOpcode(Opcode);
765 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
766 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
767 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
768 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000769 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +0000770 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
771 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
772 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +0000773
774 return;
775 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000776 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +0000777 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
778 /// in the function. The first operand is the ID# for this instruction, the
779 /// second is the index into the MachineConstantPool that this is, the third
780 /// is the size in bytes of this constant pool entry.
781 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
782 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
783
784 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +0000785 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +0000786
787 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
788 if (MCPE.isMachineConstantPoolEntry())
789 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
790 else
791 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000792
Chris Lattnera70e6442009-10-19 22:33:05 +0000793 return;
794 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000795 case ARM::MOVi2pieces: {
796 // FIXME: We'd like to remove the asm string in the .td file, but the
Chris Lattner017d9472009-10-20 00:40:56 +0000797 // This is a hack that lowers as a two instruction sequence.
798 unsigned DstReg = MI->getOperand(0).getReg();
799 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
800
801 unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
802 unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +0000803
Chris Lattner017d9472009-10-20 00:40:56 +0000804 {
805 MCInst TmpInst;
806 TmpInst.setOpcode(ARM::MOVi);
807 TmpInst.addOperand(MCOperand::CreateReg(DstReg));
808 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000809
Chris Lattner017d9472009-10-20 00:40:56 +0000810 // Predicate.
811 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
812 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Chris Lattner233917c2009-10-20 00:46:11 +0000813
814 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +0000815 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +0000816 }
817
818 {
819 MCInst TmpInst;
820 TmpInst.setOpcode(ARM::ORRri);
821 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
822 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // inreg
823 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
824 // Predicate.
825 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
826 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000827
Chris Lattner017d9472009-10-20 00:40:56 +0000828 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +0000829 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +0000830 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000831 return;
Chris Lattner017d9472009-10-20 00:40:56 +0000832 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000833 case ARM::MOVi32imm: {
834 // FIXME: We'd like to remove the asm string in the .td file, but the
Chris Lattner161dcbf2009-10-20 01:11:37 +0000835 // This is a hack that lowers as a two instruction sequence.
836 unsigned DstReg = MI->getOperand(0).getReg();
Rafael Espindola18c10212010-05-12 05:16:34 +0000837 const MachineOperand &MO = MI->getOperand(1);
838 MCOperand V1, V2;
839 if (MO.isImm()) {
840 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
841 V1 = MCOperand::CreateImm(ImmVal & 65535);
842 V2 = MCOperand::CreateImm(ImmVal >> 16);
843 } else if (MO.isGlobal()) {
Jim Grosbachc686e332010-09-17 18:25:25 +0000844 MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO.getGlobal());
Rafael Espindola18c10212010-05-12 05:16:34 +0000845 const MCSymbolRefExpr *SymRef1 =
Duncan Sands34727662010-07-12 08:16:59 +0000846 MCSymbolRefExpr::Create(Symbol,
847 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +0000848 const MCSymbolRefExpr *SymRef2 =
Duncan Sands34727662010-07-12 08:16:59 +0000849 MCSymbolRefExpr::Create(Symbol,
850 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +0000851 V1 = MCOperand::CreateExpr(SymRef1);
852 V2 = MCOperand::CreateExpr(SymRef2);
853 } else {
Jim Grosbachf0633e42010-09-22 20:55:15 +0000854 // FIXME: External symbol?
Rafael Espindola18c10212010-05-12 05:16:34 +0000855 MI->dump();
856 llvm_unreachable("cannot handle this operand");
857 }
858
Chris Lattner161dcbf2009-10-20 01:11:37 +0000859 {
860 MCInst TmpInst;
861 TmpInst.setOpcode(ARM::MOVi16);
862 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
Rafael Espindola18c10212010-05-12 05:16:34 +0000863 TmpInst.addOperand(V1); // lower16(imm)
Jim Grosbachb0739b72010-09-02 01:02:06 +0000864
Chris Lattner161dcbf2009-10-20 01:11:37 +0000865 // Predicate.
866 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
867 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000868
Chris Lattner850d2e22010-02-03 01:16:28 +0000869 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +0000870 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000871
Chris Lattner161dcbf2009-10-20 01:11:37 +0000872 {
873 MCInst TmpInst;
874 TmpInst.setOpcode(ARM::MOVTi16);
875 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
876 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // srcreg
Rafael Espindola18c10212010-05-12 05:16:34 +0000877 TmpInst.addOperand(V2); // upper16(imm)
Jim Grosbachb0739b72010-09-02 01:02:06 +0000878
Chris Lattner161dcbf2009-10-20 01:11:37 +0000879 // Predicate.
880 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
881 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbachb0739b72010-09-02 01:02:06 +0000882
Chris Lattner850d2e22010-02-03 01:16:28 +0000883 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +0000884 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000885
Chris Lattner161dcbf2009-10-20 01:11:37 +0000886 return;
887 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000888 case ARM::t2TBB:
889 case ARM::t2TBH:
890 case ARM::t2BR_JT: {
891 // Lower and emit the instruction itself, then the jump table following it.
892 MCInst TmpInst;
893 MCInstLowering.Lower(MI, TmpInst);
894 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000895 EmitJump2Table(MI);
896 return;
897 }
898 case ARM::tBR_JTr:
899 case ARM::BR_JTr:
900 case ARM::BR_JTm:
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000901 case ARM::BR_JTadd: {
902 // Lower and emit the instruction itself, then the jump table following it.
903 MCInst TmpInst;
904 MCInstLowering.Lower(MI, TmpInst);
905 OutStreamer.EmitInstruction(TmpInst);
906 EmitJumpTable(MI);
907 return;
908 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000909 case ARM::TRAP: {
910 // Non-Darwin binutils don't yet support the "trap" mnemonic.
911 // FIXME: Remove this special case when they do.
912 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000913 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +0000914 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000915 OutStreamer.AddComment("trap");
916 OutStreamer.EmitIntValue(Val, 4);
917 return;
918 }
919 break;
920 }
921 case ARM::tTRAP: {
922 // Non-Darwin binutils don't yet support the "trap" mnemonic.
923 // FIXME: Remove this special case when they do.
924 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +0000925 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +0000926 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +0000927 OutStreamer.AddComment("trap");
928 OutStreamer.EmitIntValue(Val, 2);
929 return;
930 }
931 break;
932 }
Jim Grosbach433a5782010-09-24 20:47:58 +0000933 case ARM::t2Int_eh_sjlj_setjmp:
934 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +0000935 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +0000936 // Two incoming args: GPR:$src, GPR:$val
937 // mov $val, pc
938 // adds $val, #7
939 // str $val, [$src, #4]
940 // movs r0, #0
941 // b 1f
942 // movs r0, #1
943 // 1:
944 unsigned SrcReg = MI->getOperand(0).getReg();
945 unsigned ValReg = MI->getOperand(1).getReg();
946 MCSymbol *Label = GetARMSJLJEHLabel();
947 {
948 MCInst TmpInst;
949 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
950 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
951 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
952 // 's' bit operand
953 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
954 OutStreamer.AddComment("eh_setjmp begin");
955 OutStreamer.EmitInstruction(TmpInst);
956 }
957 {
958 MCInst TmpInst;
959 TmpInst.setOpcode(ARM::tADDi3);
960 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
961 // 's' bit operand
962 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
963 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
964 TmpInst.addOperand(MCOperand::CreateImm(7));
965 // Predicate.
966 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
967 TmpInst.addOperand(MCOperand::CreateReg(0));
968 OutStreamer.EmitInstruction(TmpInst);
969 }
970 {
971 MCInst TmpInst;
972 TmpInst.setOpcode(ARM::tSTR);
973 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
974 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
975 // The offset immediate is #4. The operand value is scaled by 4 for the
976 // tSTR instruction.
977 TmpInst.addOperand(MCOperand::CreateImm(1));
978 TmpInst.addOperand(MCOperand::CreateReg(0));
979 // Predicate.
980 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
981 TmpInst.addOperand(MCOperand::CreateReg(0));
982 OutStreamer.EmitInstruction(TmpInst);
983 }
984 {
985 MCInst TmpInst;
986 TmpInst.setOpcode(ARM::tMOVi8);
987 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
988 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
989 TmpInst.addOperand(MCOperand::CreateImm(0));
990 // Predicate.
991 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
992 TmpInst.addOperand(MCOperand::CreateReg(0));
993 OutStreamer.EmitInstruction(TmpInst);
994 }
995 {
996 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
997 MCInst TmpInst;
998 TmpInst.setOpcode(ARM::tB);
999 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1000 OutStreamer.EmitInstruction(TmpInst);
1001 }
1002 {
1003 MCInst TmpInst;
1004 TmpInst.setOpcode(ARM::tMOVi8);
1005 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1006 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1007 TmpInst.addOperand(MCOperand::CreateImm(1));
1008 // Predicate.
1009 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1010 TmpInst.addOperand(MCOperand::CreateReg(0));
1011 OutStreamer.AddComment("eh_setjmp end");
1012 OutStreamer.EmitInstruction(TmpInst);
1013 }
1014 OutStreamer.EmitLabel(Label);
1015 return;
1016 }
1017
Jim Grosbach45390082010-09-23 23:33:56 +00001018 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001019 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001020 // Two incoming args: GPR:$src, GPR:$val
1021 // add $val, pc, #8
1022 // str $val, [$src, #+4]
1023 // mov r0, #0
1024 // add pc, pc, #0
1025 // mov r0, #1
1026 unsigned SrcReg = MI->getOperand(0).getReg();
1027 unsigned ValReg = MI->getOperand(1).getReg();
1028
1029 {
1030 MCInst TmpInst;
1031 TmpInst.setOpcode(ARM::ADDri);
1032 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1033 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1034 TmpInst.addOperand(MCOperand::CreateImm(8));
1035 // Predicate.
1036 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1037 TmpInst.addOperand(MCOperand::CreateReg(0));
1038 // 's' bit operand (always reg0 for this).
1039 TmpInst.addOperand(MCOperand::CreateReg(0));
1040 OutStreamer.AddComment("eh_setjmp begin");
1041 OutStreamer.EmitInstruction(TmpInst);
1042 }
1043 {
1044 MCInst TmpInst;
1045 TmpInst.setOpcode(ARM::STR);
1046 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1047 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1048 TmpInst.addOperand(MCOperand::CreateReg(0));
1049 TmpInst.addOperand(MCOperand::CreateImm(4));
1050 // Predicate.
1051 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1052 TmpInst.addOperand(MCOperand::CreateReg(0));
1053 OutStreamer.EmitInstruction(TmpInst);
1054 }
1055 {
1056 MCInst TmpInst;
1057 TmpInst.setOpcode(ARM::MOVi);
1058 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1059 TmpInst.addOperand(MCOperand::CreateImm(0));
1060 // Predicate.
1061 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1062 TmpInst.addOperand(MCOperand::CreateReg(0));
1063 // 's' bit operand (always reg0 for this).
1064 TmpInst.addOperand(MCOperand::CreateReg(0));
1065 OutStreamer.EmitInstruction(TmpInst);
1066 }
1067 {
1068 MCInst TmpInst;
1069 TmpInst.setOpcode(ARM::ADDri);
1070 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1071 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1072 TmpInst.addOperand(MCOperand::CreateImm(0));
1073 // Predicate.
1074 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1075 TmpInst.addOperand(MCOperand::CreateReg(0));
1076 // 's' bit operand (always reg0 for this).
1077 TmpInst.addOperand(MCOperand::CreateReg(0));
1078 OutStreamer.EmitInstruction(TmpInst);
1079 }
1080 {
1081 MCInst TmpInst;
1082 TmpInst.setOpcode(ARM::MOVi);
1083 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1084 TmpInst.addOperand(MCOperand::CreateImm(1));
1085 // Predicate.
1086 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1087 TmpInst.addOperand(MCOperand::CreateReg(0));
1088 // 's' bit operand (always reg0 for this).
1089 TmpInst.addOperand(MCOperand::CreateReg(0));
1090 OutStreamer.AddComment("eh_setjmp end");
1091 OutStreamer.EmitInstruction(TmpInst);
1092 }
1093 return;
1094 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001095 case ARM::Int_eh_sjlj_longjmp: {
1096 // ldr sp, [$src, #8]
1097 // ldr $scratch, [$src, #4]
1098 // ldr r7, [$src]
1099 // bx $scratch
1100 unsigned SrcReg = MI->getOperand(0).getReg();
1101 unsigned ScratchReg = MI->getOperand(1).getReg();
1102 {
1103 MCInst TmpInst;
1104 TmpInst.setOpcode(ARM::LDR);
1105 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1106 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1107 TmpInst.addOperand(MCOperand::CreateReg(0));
1108 TmpInst.addOperand(MCOperand::CreateImm(8));
1109 // Predicate.
1110 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1111 TmpInst.addOperand(MCOperand::CreateReg(0));
1112 OutStreamer.EmitInstruction(TmpInst);
1113 }
1114 {
1115 MCInst TmpInst;
1116 TmpInst.setOpcode(ARM::LDR);
1117 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1118 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1119 TmpInst.addOperand(MCOperand::CreateReg(0));
1120 TmpInst.addOperand(MCOperand::CreateImm(4));
1121 // Predicate.
1122 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1123 TmpInst.addOperand(MCOperand::CreateReg(0));
1124 OutStreamer.EmitInstruction(TmpInst);
1125 }
1126 {
1127 MCInst TmpInst;
1128 TmpInst.setOpcode(ARM::LDR);
1129 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1130 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1131 TmpInst.addOperand(MCOperand::CreateReg(0));
1132 TmpInst.addOperand(MCOperand::CreateImm(0));
1133 // Predicate.
1134 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1135 TmpInst.addOperand(MCOperand::CreateReg(0));
1136 OutStreamer.EmitInstruction(TmpInst);
1137 }
1138 {
1139 MCInst TmpInst;
1140 TmpInst.setOpcode(ARM::BRIND);
1141 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1142 // Predicate.
1143 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1144 TmpInst.addOperand(MCOperand::CreateReg(0));
1145 OutStreamer.EmitInstruction(TmpInst);
1146 }
1147 return;
1148 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001149 case ARM::tInt_eh_sjlj_longjmp: {
1150 // ldr $scratch, [$src, #8]
1151 // mov sp, $scratch
1152 // ldr $scratch, [$src, #4]
1153 // ldr r7, [$src]
1154 // bx $scratch
1155 unsigned SrcReg = MI->getOperand(0).getReg();
1156 unsigned ScratchReg = MI->getOperand(1).getReg();
1157 {
1158 MCInst TmpInst;
1159 TmpInst.setOpcode(ARM::tLDR);
1160 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1161 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1162 // The offset immediate is #8. The operand value is scaled by 4 for the
1163 // tSTR instruction.
1164 TmpInst.addOperand(MCOperand::CreateImm(2));
1165 TmpInst.addOperand(MCOperand::CreateReg(0));
1166 // Predicate.
1167 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1168 TmpInst.addOperand(MCOperand::CreateReg(0));
1169 OutStreamer.EmitInstruction(TmpInst);
1170 }
1171 {
1172 MCInst TmpInst;
1173 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1174 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1175 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1176 // Predicate.
1177 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1178 TmpInst.addOperand(MCOperand::CreateReg(0));
1179 OutStreamer.EmitInstruction(TmpInst);
1180 }
1181 {
1182 MCInst TmpInst;
1183 TmpInst.setOpcode(ARM::tLDR);
1184 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1185 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1186 TmpInst.addOperand(MCOperand::CreateImm(1));
1187 TmpInst.addOperand(MCOperand::CreateReg(0));
1188 // Predicate.
1189 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1190 TmpInst.addOperand(MCOperand::CreateReg(0));
1191 OutStreamer.EmitInstruction(TmpInst);
1192 }
1193 {
1194 MCInst TmpInst;
1195 TmpInst.setOpcode(ARM::tLDR);
1196 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1197 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1198 TmpInst.addOperand(MCOperand::CreateImm(0));
1199 TmpInst.addOperand(MCOperand::CreateReg(0));
1200 // Predicate.
1201 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1202 TmpInst.addOperand(MCOperand::CreateReg(0));
1203 OutStreamer.EmitInstruction(TmpInst);
1204 }
1205 {
1206 MCInst TmpInst;
1207 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1208 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1209 // Predicate.
1210 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1211 TmpInst.addOperand(MCOperand::CreateReg(0));
1212 OutStreamer.EmitInstruction(TmpInst);
1213 }
1214 return;
1215 }
Chris Lattner97f06932009-10-19 20:20:46 +00001216 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001217
Chris Lattner97f06932009-10-19 20:20:46 +00001218 MCInst TmpInst;
1219 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001220 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001221}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001222
1223//===----------------------------------------------------------------------===//
1224// Target Registry Stuff
1225//===----------------------------------------------------------------------===//
1226
1227static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1228 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001229 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001230 if (SyntaxVariant == 0)
Jim Grosbach74d7e6c2010-09-17 21:33:25 +00001231 return new ARMInstPrinter(MAI);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001232 return 0;
1233}
1234
1235// Force static initialization.
1236extern "C" void LLVMInitializeARMAsmPrinter() {
1237 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1238 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1239
1240 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1241 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1242}
1243