blob: 0bf6dd6e13df320116adcbeceb8be574d1cd6d9f [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"
15#include "llvm/MC/MCExpr.h"
Rafael Espindolaf230df92010-10-16 18:23:53 +000016#include "llvm/MC/MCObjectFormat.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000017#include "llvm/MC/MCObjectWriter.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000018#include "llvm/MC/MCSectionELF.h"
19#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000020#include "llvm/Object/MachOFormat.h"
Wesley Peckeecb8582010-10-22 15:52:49 +000021#include "llvm/Support/ELF.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Jim Grosbachdff84b02010-12-02 00:28:45 +000024#include "llvm/Target/TargetAsmBackend.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000025#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000026using namespace llvm;
27
28namespace {
29class ARMAsmBackend : public TargetAsmBackend {
30public:
Rafael Espindolafd467972010-11-26 04:24:21 +000031 ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000032
33 bool MayNeedRelaxation(const MCInst &Inst) const;
34
35 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
36
37 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000038
39 unsigned getPointerSize() const {
40 return 4;
41 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000042};
Chris Lattnerb75c6512010-11-17 05:41:32 +000043} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000044
45bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
46 // FIXME: Thumb targets, different move constant targets..
47 return false;
48}
49
50void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
51 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
52 return;
53}
54
55bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbache50e6bc2010-11-11 23:41:09 +000056 // FIXME: Zero fill for now. That's not right, but at least will get the
57 // section size right.
58 for (uint64_t i = 0; i != Count; ++i)
59 OW->Write8(0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000060 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000061}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000062
Jason W Kim0c628c22010-12-01 22:46:50 +000063static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
64 switch (Kind) {
65 default:
66 llvm_unreachable("Unknown fixup kind!");
67 case FK_Data_4:
Jason W Kim0c628c22010-12-01 22:46:50 +000068 return Value;
Jason W Kim2ccf1482010-12-03 19:40:23 +000069 case ARM::fixup_arm_movt_hi16:
70 case ARM::fixup_arm_movw_lo16: {
71 unsigned Hi4 = (Value & 0xF000) >> 12;
72 unsigned Lo12 = Value & 0x0FFF;
73 // inst{19-16} = Hi4;
74 // inst{11-0} = Lo12;
75 Value = (Hi4 << 16) | (Lo12);
76 return Value;
77 }
Jim Grosbachdff84b02010-12-02 00:28:45 +000078 case ARM::fixup_arm_ldst_pcrel_12: {
Jason W Kim0c628c22010-12-01 22:46:50 +000079 bool isAdd = true;
80 // ARM PC-relative values are offset by 8.
81 Value -= 8;
82 if ((int64_t)Value < 0) {
83 Value = -Value;
84 isAdd = false;
85 }
86 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
87 Value |= isAdd << 23;
88 return Value;
89 }
Jim Grosbachdff84b02010-12-02 00:28:45 +000090 case ARM::fixup_arm_adr_pcrel_12: {
91 // ARM PC-relative values are offset by 8.
92 Value -= 8;
93 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
94 if ((int64_t)Value < 0) {
95 Value = -Value;
96 opc = 2; // 0b0010
97 }
98 assert(ARM_AM::getSOImmVal(Value) != -1 &&
99 "Out of range pc-relative fixup value!");
100 // Encode the immediate and shift the opcode into place.
101 return ARM_AM::getSOImmVal(Value) | (opc << 21);
102 }
Jason W Kim0c628c22010-12-01 22:46:50 +0000103 case ARM::fixup_arm_branch:
104 // These values don't encode the low two bits since they're always zero.
105 // Offset by 8 just as above.
Jim Grosbach662a8162010-12-06 23:57:07 +0000106 return 0xffffff & ((Value - 8) >> 2);
107 case ARM::fixup_arm_thumb_bl: {
108 // The value doesn't encode the low bit (always zero) and is offset by
109 // four. The value is encoded into disjoint bit positions in the destination
110 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
111 // xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
112 // Note that the halfwords are stored high first, low second; so we need
113 // to transpose the fixup value here to map properly.
114 uint32_t Binary = 0x3fffff & ((Value - 4) >> 1);
115 Binary = ((Binary & 0x7ff) << 16) | (Binary >> 11);
116 return Binary;
117 }
Owen Andersond8e351b2010-12-08 00:18:36 +0000118 case ARM::fixup_t2_pcrel_10:
Jason W Kim0c628c22010-12-01 22:46:50 +0000119 case ARM::fixup_arm_pcrel_10: {
120 // Offset by 8 just as above.
121 Value = Value - 8;
122 bool isAdd = true;
123 if ((int64_t)Value < 0) {
124 Value = -Value;
125 isAdd = false;
126 }
127 // These values don't encode the low two bits since they're always zero.
128 Value >>= 2;
129 assert ((Value < 256) && "Out of range pc-relative fixup value!");
130 Value |= isAdd << 23;
Owen Andersond8e351b2010-12-08 00:18:36 +0000131
132 // Same addressing mode as fixup_arm_pcrel_10, but with the bytes reordered.
133 if (Kind == ARM::fixup_t2_pcrel_10) {
Owen Anderson255eafb2010-12-08 00:21:33 +0000134 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
135 swapped |= (Value & 0x0000FFFF) << 16;
Owen Andersond8e351b2010-12-08 00:18:36 +0000136 return swapped;
137 }
138
Jason W Kim0c628c22010-12-01 22:46:50 +0000139 return Value;
140 }
141 }
142}
143
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000144namespace {
Bill Wendling52e635e2010-12-07 23:05:20 +0000145
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000146// FIXME: This should be in a separate file.
147// ELF is an ELF of course...
148class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000149 MCELFObjectFormat Format;
150
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000151public:
152 Triple::OSType OSType;
153 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
154 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000155 HasScatteredSymbols = true;
156 }
157
Rafael Espindolaf230df92010-10-16 18:23:53 +0000158 virtual const MCObjectFormat &getObjectFormat() const {
159 return Format;
160 }
161
Rafael Espindola179821a2010-12-06 19:08:48 +0000162 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000163 uint64_t Value) const;
164
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000165 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000166 return createELFObjectWriter(OS, /*Is64Bit=*/false,
167 OSType, ELF::EM_ARM,
168 /*IsLittleEndian=*/true,
169 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000170 }
171};
172
Bill Wendling52e635e2010-12-07 23:05:20 +0000173// FIXME: Raise this to share code between Darwin and ELF.
Rafael Espindola179821a2010-12-06 19:08:48 +0000174void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
175 unsigned DataSize, uint64_t Value) const {
Bill Wendling52e635e2010-12-07 23:05:20 +0000176 unsigned NumBytes = 4; // FIXME: 2 for Thumb
Bill Wendling52e635e2010-12-07 23:05:20 +0000177 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000178 if (!Value) return; // Doesn't change encoding.
Bill Wendling52e635e2010-12-07 23:05:20 +0000179
180 unsigned Offset = Fixup.getOffset();
181 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
182
183 // For each byte of the fragment that the fixup touches, mask in the bits from
184 // the fixup value. The Value has been "split up" into the appropriate
185 // bitfields above.
186 for (unsigned i = 0; i != NumBytes; ++i)
187 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000188}
189
190// FIXME: This should be in a separate file.
191class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000192 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000193public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000194 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000195 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000196 }
197
Rafael Espindolaf230df92010-10-16 18:23:53 +0000198 virtual const MCObjectFormat &getObjectFormat() const {
199 return Format;
200 }
201
Rafael Espindola179821a2010-12-06 19:08:48 +0000202 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000203 uint64_t Value) const;
204
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000205 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000206 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000207 return createMachObjectWriter(OS, /*Is64Bit=*/false,
208 object::mach::CTM_ARM,
209 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000210 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000211 }
212
213 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
214 return false;
215 }
216};
217
Bill Wendlingd832fa02010-12-07 23:11:00 +0000218/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbachc466b932010-11-11 18:04:49 +0000219static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000220 switch (Kind) {
Jim Grosbach662a8162010-12-06 23:57:07 +0000221 default:
222 llvm_unreachable("Unknown fixup kind!");
223 case FK_Data_4:
224 return 4;
225 case ARM::fixup_arm_ldst_pcrel_12:
226 case ARM::fixup_arm_pcrel_10:
227 case ARM::fixup_arm_adr_pcrel_12:
228 case ARM::fixup_arm_branch:
229 return 3;
Owen Andersond8e351b2010-12-08 00:18:36 +0000230 case ARM::fixup_t2_pcrel_10:
Jim Grosbach662a8162010-12-06 23:57:07 +0000231 case ARM::fixup_arm_thumb_bl:
232 return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000233 }
234}
235
Rafael Espindola179821a2010-12-06 19:08:48 +0000236void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
237 unsigned DataSize, uint64_t Value) const {
Jim Grosbachc466b932010-11-11 18:04:49 +0000238 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000239 Value = adjustFixupValue(Fixup.getKind(), Value);
Bill Wendlingd832fa02010-12-07 23:11:00 +0000240 if (!Value) return; // Doesn't change encoding.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000241
Bill Wendlingd832fa02010-12-07 23:11:00 +0000242 unsigned Offset = Fixup.getOffset();
243 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
244
Jim Grosbach679cbd32010-11-09 01:37:15 +0000245 // For each byte of the fragment that the fixup touches, mask in the
246 // bits from the fixup value.
247 for (unsigned i = 0; i != NumBytes; ++i)
Bill Wendlingd832fa02010-12-07 23:11:00 +0000248 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000249}
Bill Wendling52e635e2010-12-07 23:05:20 +0000250
Jim Grosbachf73fd722010-09-30 03:21:00 +0000251} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000252
253TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
254 const std::string &TT) {
255 switch (Triple(TT).getOS()) {
256 case Triple::Darwin:
257 return new DarwinARMAsmBackend(T);
258 case Triple::MinGW32:
259 case Triple::Cygwin:
260 case Triple::Win32:
261 assert(0 && "Windows not supported on ARM");
262 default:
263 return new ELFARMAsmBackend(T, Triple(TT).getOS());
264 }
265}