blob: 28d902a553c9e3db178db32d4415eb5e65619762 [file] [log] [blame]
Jason W Kimd4d4f4f2010-09-30 02:17:26 +00001//===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000010#include "ARM.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000011#include "ARMAddressingModes.h"
Jim Grosbach679cbd32010-11-09 01:37:15 +000012#include "ARMFixupKinds.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000013#include "llvm/ADT/Twine.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000014#include "llvm/MC/MCAssembler.h"
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000015#include "llvm/MC/MCDirectives.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000016#include "llvm/MC/MCExpr.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000017#include "llvm/MC/MCObjectFormat.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000018#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000019#include "llvm/MC/MCSectionELF.h"
20#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000021#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000022#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000025#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000026#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000027using namespace llvm;
28
29namespace {
30class ARMAsmBackend : public TargetAsmBackend {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000031 bool isThumbMode; // Currently emitting Thumb code.
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000032public:
Jim Grosbach022ab372010-12-08 15:36:45 +000033 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000034
35 bool MayNeedRelaxation(const MCInst &Inst) const;
36
37 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
38
39 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000040
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000041 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
42 switch (Flag) {
43 default: break;
44 case MCAF_Code16:
45 setIsThumb(true);
46 break;
47 case MCAF_Code32:
48 setIsThumb(false);
49 break;
50 }
Jim Grosbach3787a402010-09-30 17:45:51 +000051 }
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000052
53 unsigned getPointerSize() const { return 4; }
54 bool isThumb() const { return isThumbMode; }
55 void setIsThumb(bool it) { isThumbMode = it; }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000056};
Chris Lattnerb75c6512010-11-17 05:41:32 +000057} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000058
59bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
60 // FIXME: Thumb targets, different move constant targets..
61 return false;
62}
63
64void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
66 return;
67}
68
69bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000070 if (isThumb()) {
71 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
72 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
73 // use 0x46c0 (which is a 'mov r8, r8' insn).
74 Count /= 2;
75 for (uint64_t i = 0; i != Count; ++i)
76 OW->Write16(0xbf00);
77 return true;
78 }
79 // ARM mode
80 Count /= 4;
Jim Grosbache50e6bc2010-11-11 23:41:09 +000081 for (uint64_t i = 0; i != Count; ++i)
Jim Grosbach5be6d2a2010-12-08 01:16:55 +000082 OW->Write32(0xe1a00000);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000083 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000084}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000085
Jason W Kim0c628c22010-12-01 22:46:50 +000086static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
87 switch (Kind) {
88 default:
89 llvm_unreachable("Unknown fixup kind!");
90 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +000091 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +000092 case ARM::fixup_arm_movt_hi16:
93 case ARM::fixup_arm_movw_lo16: {
94 unsigned Hi4 = (Value & 0xF000) >> 12;
95 unsigned Lo12 = Value & 0x0FFF;
96 // inst{19-16} = Hi4;
97 // inst{11-0} = Lo12;
98 Value = (Hi4 << 16) | (Lo12);
99 return Value;
100 }
Jim Grosbachdff84b02010-12-02 00:28:45 +0000101 case ARM::fixup_arm_ldst_pcrel_12: {
Jason W Kim0c628c22010-12-01 22:46:50 +0000102 bool isAdd = true;
103 // ARM PC-relative values are offset by 8.
104 Value -= 8;
105 if ((int64_t)Value < 0) {
106 Value = -Value;
107 isAdd = false;
108 }
109 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
110 Value |= isAdd << 23;
111 return Value;
112 }
Jim Grosbachdff84b02010-12-02 00:28:45 +0000113 case ARM::fixup_arm_adr_pcrel_12: {
114 // ARM PC-relative values are offset by 8.
115 Value -= 8;
116 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
117 if ((int64_t)Value < 0) {
118 Value = -Value;
119 opc = 2; // 0b0010
120 }
121 assert(ARM_AM::getSOImmVal(Value) != -1 &&
122 "Out of range pc-relative fixup value!");
123 // Encode the immediate and shift the opcode into place.
124 return ARM_AM::getSOImmVal(Value) | (opc << 21);
125 }
Jason W Kim0c628c22010-12-01 22:46:50 +0000126 case ARM::fixup_arm_branch:
127 // These values don't encode the low two bits since they're always zero.
128 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000129 return 0xffffff & ((Value - 8) >> 2);
130 case ARM::fixup_arm_thumb_bl: {
131 // The value doesn't encode the low bit (always zero) and is offset by
132 // four. The value is encoded into disjoint bit positions in the destination
133 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
134 // xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
135 // Note that the halfwords are stored high first, low second; so we need
136 // to transpose the fixup value here to map properly.
137 uint32_t Binary = 0x3fffff & ((Value - 4) >> 1);
138 Binary = ((Binary & 0x7ff) << 16) | (Binary >> 11);
139 return Binary;
140 }
Bill Wendlingb8958b02010-12-08 01:57:09 +0000141 case ARM::fixup_arm_thumb_cp:
142 // Offset by 4, and don't encode the low two bits.
143 return ((Value - 4) >> 2) & 0xff;
Owen Andersond8e351b2010-12-08 00:18:36 +0000144 case ARM::fixup_t2_pcrel_10:
Jason W Kim0c628c22010-12-01 22:46:50 +0000145 case ARM::fixup_arm_pcrel_10: {
146 // Offset by 8 just as above.
147 Value = Value - 8;
148 bool isAdd = true;
149 if ((int64_t)Value < 0) {
150 Value = -Value;
151 isAdd = false;
152 }
153 // These values don't encode the low two bits since they're always zero.
154 Value >>= 2;
155 assert ((Value < 256) && "Out of range pc-relative fixup value!");
156 Value |= isAdd << 23;
Owen Andersond8e351b2010-12-08 00:18:36 +0000157
Owen Andersoncc78f5c2010-12-08 19:31:11 +0000158 // Same addressing mode as fixup_arm_pcrel_10,
159 // but with 16-bit halfwords swapped.
Owen Andersond8e351b2010-12-08 00:18:36 +0000160 if (Kind == ARM::fixup_t2_pcrel_10) {
Owen Anderson255eafb2010-12-08 00:21:33 +0000161 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
162 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000163 return swapped;
164 }
165
Jason W Kim0c628c22010-12-01 22:46:50 +0000166 return Value;
167 }
168 }
169}
170
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000171namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000172
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000173// FIXME: This should be in a separate file.
174// ELF is an ELF of course...
175class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000176 MCELFObjectFormat Format;
177
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000178public:
179 Triple::OSType OSType;
180 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
181 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000182 HasScatteredSymbols = true;
183 }
184
Rafael Espindolaf230df92010-10-16 18:23:53 +0000185 virtual const MCObjectFormat &getObjectFormat() const {
186 return Format;
187 }
188
Rafael Espindola179821a2010-12-06 19:08:48 +0000189 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000190 uint64_t Value) const;
191
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000192 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000193 return createELFObjectWriter(OS, /*Is64Bit=*/false,
194 OSType, ELF::EM_ARM,
195 /*IsLittleEndian=*/true,
196 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000197 }
198};
199
Bill Wendling52e635e2010-12-07 23:05:20 +0000200// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000201void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
202 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000203 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000204 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000205 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000206
207 unsigned Offset = Fixup.getOffset();
208 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
209
210 // For each byte of the fragment that the fixup touches, mask in the bits from
211 // the fixup value. The Value has been "split up" into the appropriate
212 // bitfields above.
213 for (unsigned i = 0; i != NumBytes; ++i)
214 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000215}
216
217// FIXME: This should be in a separate file.
218class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000219 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000220public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000221 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000222 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000223 }
224
Rafael Espindolaf230df92010-10-16 18:23:53 +0000225 virtual const MCObjectFormat &getObjectFormat() const {
226 return Format;
227 }
228
Rafael Espindola179821a2010-12-06 19:08:48 +0000229 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000230 uint64_t Value) const;
231
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000232 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000233 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000234 return createMachObjectWriter(OS, /*Is64Bit=*/false,
235 object::mach::CTM_ARM,
236 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000237 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000238 }
239
240 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
241 return false;
242 }
243};
244
Bill Wendlingd832fa02010-12-07 23:11:00 +0000245/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000246static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000247 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000248 default:
249 llvm_unreachable("Unknown fixup kind!");
Bill Wendlingb8958b02010-12-08 01:57:09 +0000250
251 case ARM::fixup_arm_thumb_cp:
252 return 1;
253
Jim Grosbach662a8162010-12-06 23:57:07 +0000254 case ARM::fixup_arm_ldst_pcrel_12:
255 case ARM::fixup_arm_pcrel_10:
256 case ARM::fixup_arm_adr_pcrel_12:
257 case ARM::fixup_arm_branch:
258 return 3;
Bill Wendlingb8958b02010-12-08 01:57:09 +0000259
260 case FK_Data_4:
Owen Andersond8e351b2010-12-08 00:18:36 +0000261 case ARM::fixup_t2_pcrel_10:
Jim Grosbach662a8162010-12-06 23:57:07 +0000262 case ARM::fixup_arm_thumb_bl:
263 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000264 }
265}
266
Rafael Espindola179821a2010-12-06 19:08:48 +0000267void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
268 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000269 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000270 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000271 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000272
Bill Wendlingd832fa02010-12-07 23:11:00 +0000273 unsigned Offset = Fixup.getOffset();
274 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
275
Jim Grosbach679cbd32010-11-09 01:37:15 +0000276 // For each byte of the fragment that the fixup touches, mask in the
277 // bits from the fixup value.
278 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000279 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000280}
Bill Wendling52e635e2010-12-07 23:05:20 +0000281
Jim Grosbachf73fd722010-09-30 03:21:00 +0000282} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000283
284TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
285 const std::string &TT) {
286 switch (Triple(TT).getOS()) {
287 case Triple::Darwin:
288 return new DarwinARMAsmBackend(T);
289 case Triple::MinGW32:
290 case Triple::Cygwin:
291 case Triple::Win32:
292 assert(0 && "Windows not supported on ARM");
293 default:
294 return new ELFARMAsmBackend(T, Triple(TT).getOS());
295 }
296}