1 //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Target/TargetAsmBackend.h"
12 #include "ARMFixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectFormat.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/Object/MachOFormat.h"
21 #include "llvm/Support/ELF.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetRegistry.h"
28 class ARMAsmBackend : public TargetAsmBackend {
30 ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
32 bool MayNeedRelaxation(const MCInst &Inst) const;
34 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
36 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
38 unsigned getPointerSize() const {
42 } // end anonymous namespace
44 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
45 // FIXME: Thumb targets, different move constant targets..
49 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
50 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
54 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
55 // 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)
62 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
65 llvm_unreachable("Unknown fixup kind!");
67 case ARM::fixup_arm_movt_hi16:
68 case ARM::fixup_arm_movw_lo16:
70 case ARM::fixup_arm_pcrel_12: {
72 // ARM PC-relative values are offset by 8.
74 if ((int64_t)Value < 0) {
78 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
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.
90 if ((int64_t)Value < 0) {
94 // These values don't encode the low two bits since they're always zero.
96 assert ((Value < 256) && "Out of range pc-relative fixup value!");
104 // FIXME: This should be in a separate file.
105 // ELF is an ELF of course...
106 class ELFARMAsmBackend : public ARMAsmBackend {
107 MCELFObjectFormat Format;
110 Triple::OSType OSType;
111 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
112 : ARMAsmBackend(T), OSType(_OSType) {
113 HasScatteredSymbols = true;
116 virtual const MCObjectFormat &getObjectFormat() const {
120 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
121 uint64_t Value) const;
123 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
124 return createELFObjectWriter(OS, /*Is64Bit=*/false,
126 /*IsLittleEndian=*/true,
127 /*HasRelocationAddend=*/false);
131 // Fixme: can we raise this to share code between Darwin and ELF?
132 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
133 uint64_t Value) const {
135 // Fixme: 2 for Thumb
136 unsigned NumBytes = 4;
137 Value = adjustFixupValue(Fixup.getKind(), Value);
139 switch (Fixup.getKind()) {
140 default: assert(0 && "Unsupported Fixup kind"); break;
141 case ARM::fixup_arm_branch: {
142 unsigned Lo24 = Value & 0xFFFFFF;
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);
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));
170 // FIXME: This should be in a separate file.
171 class DarwinARMAsmBackend : public ARMAsmBackend {
172 MCMachOObjectFormat Format;
174 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
175 HasScatteredSymbols = true;
178 virtual const MCObjectFormat &getObjectFormat() const {
182 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
183 uint64_t Value) const;
185 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
186 // FIXME: Subtarget info should be derived. Force v7 for now.
187 return createMachObjectWriter(OS, /*Is64Bit=*/false,
188 object::mach::CTM_ARM,
189 object::mach::CSARM_V7,
190 /*IsLittleEndian=*/true);
193 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
197 } // end anonymous namespace
199 static unsigned getFixupKindNumBytes(unsigned Kind) {
201 default: llvm_unreachable("Unknown fixup kind!");
202 case FK_Data_4: return 4;
203 case ARM::fixup_arm_pcrel_12: return 3;
204 case ARM::fixup_arm_pcrel_10: return 3;
205 case ARM::fixup_arm_branch: return 3;
209 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
210 uint64_t Value) const {
211 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
212 Value = adjustFixupValue(Fixup.getKind(), Value);
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));
221 } // end anonymous namespace
223 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
224 const std::string &TT) {
225 switch (Triple(TT).getOS()) {
227 return new DarwinARMAsmBackend(T);
228 case Triple::MinGW32:
231 assert(0 && "Windows not supported on ARM");
233 return new ELFARMAsmBackend(T, Triple(TT).getOS());