blob: eb03a24b026e19e519f6cf9bfcf599e91ec5f4fb [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:
68 case ARM::fixup_arm_movt_hi16:
69 case ARM::fixup_arm_movw_lo16:
70 return Value;
Jim Grosbachdff84b02010-12-02 00:28:45 +000071 case ARM::fixup_arm_ldst_pcrel_12: {
Jason W Kim0c628c22010-12-01 22:46:50 +000072 bool isAdd = true;
73 // ARM PC-relative values are offset by 8.
74 Value -= 8;
75 if ((int64_t)Value < 0) {
76 Value = -Value;
77 isAdd = false;
78 }
79 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
80 Value |= isAdd << 23;
81 return Value;
82 }
Jim Grosbachdff84b02010-12-02 00:28:45 +000083 case ARM::fixup_arm_adr_pcrel_12: {
84 // ARM PC-relative values are offset by 8.
85 Value -= 8;
86 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
87 if ((int64_t)Value < 0) {
88 Value = -Value;
89 opc = 2; // 0b0010
90 }
91 assert(ARM_AM::getSOImmVal(Value) != -1 &&
92 "Out of range pc-relative fixup value!");
93 // Encode the immediate and shift the opcode into place.
94 return ARM_AM::getSOImmVal(Value) | (opc << 21);
95 }
Jason W Kim0c628c22010-12-01 22:46:50 +000096 case ARM::fixup_arm_branch:
97 // These values don't encode the low two bits since they're always zero.
98 // Offset by 8 just as above.
99 return (Value - 8) >> 2;
100 case ARM::fixup_arm_pcrel_10: {
101 // Offset by 8 just as above.
102 Value = Value - 8;
103 bool isAdd = true;
104 if ((int64_t)Value < 0) {
105 Value = -Value;
106 isAdd = false;
107 }
108 // These values don't encode the low two bits since they're always zero.
109 Value >>= 2;
110 assert ((Value < 256) && "Out of range pc-relative fixup value!");
111 Value |= isAdd << 23;
112 return Value;
113 }
114 }
115}
116
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000117namespace {
118// FIXME: This should be in a separate file.
119// ELF is an ELF of course...
120class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000121 MCELFObjectFormat Format;
122
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000123public:
124 Triple::OSType OSType;
125 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
126 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000127 HasScatteredSymbols = true;
128 }
129
Rafael Espindolaf230df92010-10-16 18:23:53 +0000130 virtual const MCObjectFormat &getObjectFormat() const {
131 return Format;
132 }
133
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000134 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
135 uint64_t Value) const;
136
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000137 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000138 return createELFObjectWriter(OS, /*Is64Bit=*/false,
139 OSType, ELF::EM_ARM,
140 /*IsLittleEndian=*/true,
141 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000142 }
143};
144
Jason W Kima4c27242010-09-30 14:58:19 +0000145// Fixme: can we raise this to share code between Darwin and ELF?
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000146void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
147 uint64_t Value) const {
Jason W Kim85fed5e2010-12-01 02:40:06 +0000148 uint32_t Mask = 0;
149 // Fixme: 2 for Thumb
Jason W Kim1920d822010-12-01 19:07:22 +0000150 unsigned NumBytes = 4;
Jason W Kim85fed5e2010-12-01 02:40:06 +0000151 Value = adjustFixupValue(Fixup.getKind(), Value);
152
153 switch (Fixup.getKind()) {
154 default: assert(0 && "Unsupported Fixup kind"); break;
155 case ARM::fixup_arm_branch: {
156 unsigned Lo24 = Value & 0xFFFFFF;
157 Mask = ~(0xFFFFFF);
158 Value = Lo24;
159 }; break;
160 case ARM::fixup_arm_movt_hi16:
161 case ARM::fixup_arm_movw_lo16: {
162 unsigned Hi4 = (Value & 0xF000) >> 12;
163 unsigned Lo12 = Value & 0x0FFF;
164 // inst{19-16} = Hi4;
165 // inst{11-0} = Lo12;
166 Value = (Hi4 << 16) | (Lo12);
167 Mask = ~(0xF0FFF);
168 }; break;
169 }
170
171 assert((Fixup.getOffset() % NumBytes == 0)
172 && "Offset mod NumBytes is nonzero!");
173 // For each byte of the fragment that the fixup touches, mask in the
174 // bits from the fixup value.
175 // The Value has been "split up" into the appropriate bitfields above.
176 // Fixme: how to share code with the .td generated code?
177 for (unsigned i = 0; i != NumBytes; ++i) {
178 DF.getContents()[Fixup.getOffset() + i] &= uint8_t(Mask >> (i * 8));
179 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
180 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000181}
182
Chris Lattnerb75c6512010-11-17 05:41:32 +0000183namespace {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000184// FIXME: This should be in a separate file.
185class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000186 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000187public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000188 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000189 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000190 }
191
Rafael Espindolaf230df92010-10-16 18:23:53 +0000192 virtual const MCObjectFormat &getObjectFormat() const {
193 return Format;
194 }
195
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000196 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
197 uint64_t Value) const;
198
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000199 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000200 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000201 return createMachObjectWriter(OS, /*Is64Bit=*/false,
202 object::mach::CTM_ARM,
203 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000204 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000205 }
206
207 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
208 return false;
209 }
210};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000211} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000212
Jim Grosbachc466b932010-11-11 18:04:49 +0000213static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000214 switch (Kind) {
215 default: llvm_unreachable("Unknown fixup kind!");
Jim Grosbachc466b932010-11-11 18:04:49 +0000216 case FK_Data_4: return 4;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000217 case ARM::fixup_arm_ldst_pcrel_12: return 3;
Owen Anderson9d63d902010-12-01 19:18:46 +0000218 case ARM::fixup_arm_pcrel_10: return 3;
Jim Grosbachdff84b02010-12-02 00:28:45 +0000219 case ARM::fixup_arm_adr_pcrel_12: return 3;
Jim Grosbachc466b932010-11-11 18:04:49 +0000220 case ARM::fixup_arm_branch: return 3;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000221 }
222}
223
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000224void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Jim Grosbachc466b932010-11-11 18:04:49 +0000225 uint64_t Value) const {
226 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000227 Value = adjustFixupValue(Fixup.getKind(), Value);
228
229 assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
230 "Invalid fixup offset!");
231 // For each byte of the fragment that the fixup touches, mask in the
232 // bits from the fixup value.
233 for (unsigned i = 0; i != NumBytes; ++i)
234 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000235}
Jim Grosbachf73fd722010-09-30 03:21:00 +0000236} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000237
238TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
239 const std::string &TT) {
240 switch (Triple(TT).getOS()) {
241 case Triple::Darwin:
242 return new DarwinARMAsmBackend(T);
243 case Triple::MinGW32:
244 case Triple::Cygwin:
245 case Triple::Win32:
246 assert(0 && "Windows not supported on ARM");
247 default:
248 return new ELFARMAsmBackend(T, Triple(TT).getOS());
249 }
250}