blob: 0672899d7b32499aabc1ae80149393a2f5fac315 [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
10#include "llvm/Target/TargetAsmBackend.h"
11#include "ARM.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"
24#include "llvm/Target/TargetRegistry.h"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000025using namespace llvm;
26
27namespace {
28class ARMAsmBackend : public TargetAsmBackend {
29public:
Rafael Espindolafd467972010-11-26 04:24:21 +000030 ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000031
32 bool MayNeedRelaxation(const MCInst &Inst) const;
33
34 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
35
36 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000037
38 unsigned getPointerSize() const {
39 return 4;
40 }
Jason W Kim85fed5e2010-12-01 02:40:06 +000041
42protected:
43 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
44 switch (Kind) {
45 default:
46 llvm_unreachable("Unknown fixup kind!");
47 case FK_Data_4:
48 case ARM::fixup_arm_movt_hi16:
49 case ARM::fixup_arm_movw_lo16:
50 return Value;
51 case ARM::fixup_arm_pcrel_12: {
52 bool isAdd = true;
53 // ARM PC-relative values are offset by 8.
54 Value -= 8;
55 if ((int64_t)Value < 0) {
56 Value = -Value;
57 isAdd = false;
58 }
59 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
60 Value |= isAdd << 23;
61 return Value;
62 }
63 case ARM::fixup_arm_branch:
64 // These values don't encode the low two bits since they're always zero.
65 // Offset by 8 just as above.
66 return (Value - 8) >> 2;
Owen Anderson9d63d902010-12-01 19:18:46 +000067 case ARM::fixup_arm_pcrel_10: {
Jason W Kim85fed5e2010-12-01 02:40:06 +000068 // Offset by 8 just as above.
69 Value = Value - 8;
70 bool isAdd = true;
71 if ((int64_t)Value < 0) {
72 Value = -Value;
73 isAdd = false;
74 }
75 // These values don't encode the low two bits since they're always zero.
76 Value >>= 2;
77 assert ((Value < 256) && "Out of range pc-relative fixup value!");
78 Value |= isAdd << 23;
79 return Value;
80 }
81 }
82 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000083};
Chris Lattnerb75c6512010-11-17 05:41:32 +000084} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000085
86bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
87 // FIXME: Thumb targets, different move constant targets..
88 return false;
89}
90
91void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
92 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
93 return;
94}
95
96bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbache50e6bc2010-11-11 23:41:09 +000097 // FIXME: Zero fill for now. That's not right, but at least will get the
98 // section size right.
99 for (uint64_t i = 0; i != Count; ++i)
100 OW->Write8(0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000101 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +0000102}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000103
104namespace {
105// FIXME: This should be in a separate file.
106// ELF is an ELF of course...
107class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000108 MCELFObjectFormat Format;
109
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000110public:
111 Triple::OSType OSType;
112 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
113 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000114 HasScatteredSymbols = true;
115 }
116
Rafael Espindolaf230df92010-10-16 18:23:53 +0000117 virtual const MCObjectFormat &getObjectFormat() const {
118 return Format;
119 }
120
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000121 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
122 uint64_t Value) const;
123
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000124 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000125 return createELFObjectWriter(OS, /*Is64Bit=*/false,
126 OSType, ELF::EM_ARM,
127 /*IsLittleEndian=*/true,
128 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000129 }
130};
131
Jason W Kima4c27242010-09-30 14:58:19 +0000132// Fixme: can we raise this to share code between Darwin and ELF?
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000133void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
134 uint64_t Value) const {
Jason W Kim85fed5e2010-12-01 02:40:06 +0000135 uint32_t Mask = 0;
136 // Fixme: 2 for Thumb
Jason W Kim1920d822010-12-01 19:07:22 +0000137 unsigned NumBytes = 4;
Jason W Kim85fed5e2010-12-01 02:40:06 +0000138 Value = adjustFixupValue(Fixup.getKind(), Value);
139
140 switch (Fixup.getKind()) {
141 default: assert(0 && "Unsupported Fixup kind"); break;
142 case ARM::fixup_arm_branch: {
143 unsigned Lo24 = Value & 0xFFFFFF;
144 Mask = ~(0xFFFFFF);
145 Value = Lo24;
146 }; break;
147 case ARM::fixup_arm_movt_hi16:
148 case ARM::fixup_arm_movw_lo16: {
149 unsigned Hi4 = (Value & 0xF000) >> 12;
150 unsigned Lo12 = Value & 0x0FFF;
151 // inst{19-16} = Hi4;
152 // inst{11-0} = Lo12;
153 Value = (Hi4 << 16) | (Lo12);
154 Mask = ~(0xF0FFF);
155 }; break;
156 }
157
158 assert((Fixup.getOffset() % NumBytes == 0)
159 && "Offset mod NumBytes is nonzero!");
160 // For each byte of the fragment that the fixup touches, mask in the
161 // bits from the fixup value.
162 // The Value has been "split up" into the appropriate bitfields above.
163 // Fixme: how to share code with the .td generated code?
164 for (unsigned i = 0; i != NumBytes; ++i) {
165 DF.getContents()[Fixup.getOffset() + i] &= uint8_t(Mask >> (i * 8));
166 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
167 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000168}
169
Chris Lattnerb75c6512010-11-17 05:41:32 +0000170namespace {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000171// FIXME: This should be in a separate file.
172class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000173 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000174public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000175 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000176 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000177 }
178
Rafael Espindolaf230df92010-10-16 18:23:53 +0000179 virtual const MCObjectFormat &getObjectFormat() const {
180 return Format;
181 }
182
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000183 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
184 uint64_t Value) const;
185
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000186 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000187 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000188 return createMachObjectWriter(OS, /*Is64Bit=*/false,
189 object::mach::CTM_ARM,
190 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000191 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000192 }
193
194 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
195 return false;
196 }
197};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000198} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000199
Jim Grosbachc466b932010-11-11 18:04:49 +0000200static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000201 switch (Kind) {
202 default: llvm_unreachable("Unknown fixup kind!");
Jim Grosbachc466b932010-11-11 18:04:49 +0000203 case FK_Data_4: return 4;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000204 case ARM::fixup_arm_pcrel_12: return 3;
Owen Anderson9d63d902010-12-01 19:18:46 +0000205 case ARM::fixup_arm_pcrel_10: return 3;
Jim Grosbachc466b932010-11-11 18:04:49 +0000206 case ARM::fixup_arm_branch: return 3;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000207 }
208}
209
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000210void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Jim Grosbachc466b932010-11-11 18:04:49 +0000211 uint64_t Value) const {
212 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000213 Value = adjustFixupValue(Fixup.getKind(), Value);
214
215 assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
216 "Invalid fixup offset!");
217 // For each byte of the fragment that the fixup touches, mask in the
218 // bits from the fixup value.
219 for (unsigned i = 0; i != NumBytes; ++i)
220 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000221}
Jim Grosbachf73fd722010-09-30 03:21:00 +0000222} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000223
224TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
225 const std::string &TT) {
226 switch (Triple(TT).getOS()) {
227 case Triple::Darwin:
228 return new DarwinARMAsmBackend(T);
229 case Triple::MinGW32:
230 case Triple::Cygwin:
231 case Triple::Win32:
232 assert(0 && "Windows not supported on ARM");
233 default:
234 return new ELFARMAsmBackend(T, Triple(TT).getOS());
235 }
236}