blob: c07a81606841a45a474a271dd26a85fda1673453 [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 Kimd4d4f4f2010-09-30 02:17:26 +000041};
Chris Lattnerb75c6512010-11-17 05:41:32 +000042} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000043
44bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
45 // FIXME: Thumb targets, different move constant targets..
46 return false;
47}
48
49void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
50 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
51 return;
52}
53
54bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbache50e6bc2010-11-11 23:41:09 +000055 // FIXME: Zero fill for now. That's not right, but at least will get the
56 // section size right.
57 for (uint64_t i = 0; i != Count; ++i)
58 OW->Write8(0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000059 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000060}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000061
Jason W Kim0c628c22010-12-01 22:46:50 +000062static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
63 switch (Kind) {
64 default:
65 llvm_unreachable("Unknown fixup kind!");
66 case FK_Data_4:
67 case ARM::fixup_arm_movt_hi16:
68 case ARM::fixup_arm_movw_lo16:
69 return Value;
70 case ARM::fixup_arm_pcrel_12: {
71 bool isAdd = true;
72 // ARM PC-relative values are offset by 8.
73 Value -= 8;
74 if ((int64_t)Value < 0) {
75 Value = -Value;
76 isAdd = false;
77 }
78 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
79 Value |= isAdd << 23;
80 return Value;
81 }
82 case ARM::fixup_arm_branch:
83 // These values don't encode the low two bits since they're always zero.
84 // Offset by 8 just as above.
85 return (Value - 8) >> 2;
86 case ARM::fixup_arm_pcrel_10: {
87 // Offset by 8 just as above.
88 Value = Value - 8;
89 bool isAdd = true;
90 if ((int64_t)Value < 0) {
91 Value = -Value;
92 isAdd = false;
93 }
94 // These values don't encode the low two bits since they're always zero.
95 Value >>= 2;
96 assert ((Value < 256) && "Out of range pc-relative fixup value!");
97 Value |= isAdd << 23;
98 return Value;
99 }
100 }
101}
102
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000103namespace {
104// FIXME: This should be in a separate file.
105// ELF is an ELF of course...
106class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000107 MCELFObjectFormat Format;
108
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000109public:
110 Triple::OSType OSType;
111 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
112 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000113 HasScatteredSymbols = true;
114 }
115
Rafael Espindolaf230df92010-10-16 18:23:53 +0000116 virtual const MCObjectFormat &getObjectFormat() const {
117 return Format;
118 }
119
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000120 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
121 uint64_t Value) const;
122
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000123 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000124 return createELFObjectWriter(OS, /*Is64Bit=*/false,
125 OSType, ELF::EM_ARM,
126 /*IsLittleEndian=*/true,
127 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000128 }
129};
130
Jason W Kima4c27242010-09-30 14:58:19 +0000131// Fixme: can we raise this to share code between Darwin and ELF?
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000132void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
133 uint64_t Value) const {
Jason W Kim85fed5e2010-12-01 02:40:06 +0000134 uint32_t Mask = 0;
135 // Fixme: 2 for Thumb
Jason W Kim1920d822010-12-01 19:07:22 +0000136 unsigned NumBytes = 4;
Jason W Kim85fed5e2010-12-01 02:40:06 +0000137 Value = adjustFixupValue(Fixup.getKind(), Value);
138
139 switch (Fixup.getKind()) {
140 default: assert(0 && "Unsupported Fixup kind"); break;
141 case ARM::fixup_arm_branch: {
142 unsigned Lo24 = Value & 0xFFFFFF;
143 Mask = ~(0xFFFFFF);
144 Value = Lo24;
145 }; break;
146 case ARM::fixup_arm_movt_hi16:
147 case ARM::fixup_arm_movw_lo16: {
148 unsigned Hi4 = (Value & 0xF000) >> 12;
149 unsigned Lo12 = Value & 0x0FFF;
150 // inst{19-16} = Hi4;
151 // inst{11-0} = Lo12;
152 Value = (Hi4 << 16) | (Lo12);
153 Mask = ~(0xF0FFF);
154 }; break;
155 }
156
157 assert((Fixup.getOffset() % NumBytes == 0)
158 && "Offset mod NumBytes is nonzero!");
159 // For each byte of the fragment that the fixup touches, mask in the
160 // bits from the fixup value.
161 // The Value has been "split up" into the appropriate bitfields above.
162 // Fixme: how to share code with the .td generated code?
163 for (unsigned i = 0; i != NumBytes; ++i) {
164 DF.getContents()[Fixup.getOffset() + i] &= uint8_t(Mask >> (i * 8));
165 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
166 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000167}
168
Chris Lattnerb75c6512010-11-17 05:41:32 +0000169namespace {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000170// FIXME: This should be in a separate file.
171class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000172 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000173public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000174 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000175 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000176 }
177
Rafael Espindolaf230df92010-10-16 18:23:53 +0000178 virtual const MCObjectFormat &getObjectFormat() const {
179 return Format;
180 }
181
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000182 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
183 uint64_t Value) const;
184
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000185 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000186 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000187 return createMachObjectWriter(OS, /*Is64Bit=*/false,
188 object::mach::CTM_ARM,
189 object::mach::CSARM_V7,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000190 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000191 }
192
193 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
194 return false;
195 }
196};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000197} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000198
Jim Grosbachc466b932010-11-11 18:04:49 +0000199static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000200 switch (Kind) {
201 default: llvm_unreachable("Unknown fixup kind!");
Jim Grosbachc466b932010-11-11 18:04:49 +0000202 case FK_Data_4: return 4;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000203 case ARM::fixup_arm_pcrel_12: return 3;
Owen Anderson9d63d902010-12-01 19:18:46 +0000204 case ARM::fixup_arm_pcrel_10: return 3;
Jim Grosbachc466b932010-11-11 18:04:49 +0000205 case ARM::fixup_arm_branch: return 3;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000206 }
207}
208
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000209void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Jim Grosbachc466b932010-11-11 18:04:49 +0000210 uint64_t Value) const {
211 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000212 Value = adjustFixupValue(Fixup.getKind(), Value);
213
214 assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
215 "Invalid fixup offset!");
216 // For each byte of the fragment that the fixup touches, mask in the
217 // bits from the fixup value.
218 for (unsigned i = 0; i != NumBytes; ++i)
219 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000220}
Jim Grosbachf73fd722010-09-30 03:21:00 +0000221} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000222
223TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
224 const std::string &TT) {
225 switch (Triple(TT).getOS()) {
226 case Triple::Darwin:
227 return new DarwinARMAsmBackend(T);
228 case Triple::MinGW32:
229 case Triple::Cygwin:
230 case Triple::Win32:
231 assert(0 && "Windows not supported on ARM");
232 default:
233 return new ELFARMAsmBackend(T, Triple(TT).getOS());
234 }
235}