blob: 86b1e069a7c5065cdaa20a54113abd8dab953bc5 [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"
Wesley Peckeecb8582010-10-22 15:52:49 +000020#include "llvm/Support/ELF.h"
Jim Grosbachc9d14392010-11-05 18:48:58 +000021#include "llvm/Support/MachO.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"
25#include "llvm/Target/TargetAsmBackend.h"
26using namespace llvm;
27
28namespace {
29class ARMAsmBackend : public TargetAsmBackend {
30public:
31 ARMAsmBackend(const Target &T)
Jim Grosbachf73fd722010-09-30 03:21:00 +000032 : TargetAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000033 }
34
35 bool MayNeedRelaxation(const MCInst &Inst) const;
36
37 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
38
39 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
Jim Grosbach3787a402010-09-30 17:45:51 +000040
41 unsigned getPointerSize() const {
42 return 4;
43 }
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000044};
45
46bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
47 // FIXME: Thumb targets, different move constant targets..
48 return false;
49}
50
51void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
52 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
53 return;
54}
55
56bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbache50e6bc2010-11-11 23:41:09 +000057// if ((Count % 4) != 0) {
58// // Fixme: % 2 for Thumb?
59// return false;
60// }
61 // FIXME: Zero fill for now. That's not right, but at least will get the
62 // section size right.
63 for (uint64_t i = 0; i != Count; ++i)
64 OW->Write8(0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000065 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000066}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000067} // end anonymous namespace
68
69namespace {
70// FIXME: This should be in a separate file.
71// ELF is an ELF of course...
72class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +000073 MCELFObjectFormat Format;
74
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000075public:
76 Triple::OSType OSType;
77 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
78 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000079 HasScatteredSymbols = true;
80 }
81
Rafael Espindolaf230df92010-10-16 18:23:53 +000082 virtual const MCObjectFormat &getObjectFormat() const {
83 return Format;
84 }
85
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000086 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
87 uint64_t Value) const;
88
89 bool isVirtualSection(const MCSection &Section) const {
90 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
91 return SE.getType() == MCSectionELF::SHT_NOBITS;
92 }
93
94 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000095 return createELFObjectWriter(OS, /*Is64Bit=*/false,
96 OSType, ELF::EM_ARM,
97 /*IsLittleEndian=*/true,
98 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000099 }
100};
101
Jason W Kima4c27242010-09-30 14:58:19 +0000102// Fixme: can we raise this to share code between Darwin and ELF?
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000103void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
104 uint64_t Value) const {
105 assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented");
106}
107
108// FIXME: This should be in a separate file.
109class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000110 MCMachOObjectFormat Format;
111
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000112public:
113 DarwinARMAsmBackend(const Target &T)
114 : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000115 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000116 }
117
Rafael Espindolaf230df92010-10-16 18:23:53 +0000118 virtual const MCObjectFormat &getObjectFormat() const {
119 return Format;
120 }
121
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000122 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
123 uint64_t Value) const;
124
125 bool isVirtualSection(const MCSection &Section) const {
126 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
127 return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
128 SMO.getType() == MCSectionMachO::S_GB_ZEROFILL ||
129 SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
130 }
131
132 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000133 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000134 return createMachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeARM,
135 MachO::CPUSubType_ARM_V7,
136 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000137 }
138
139 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
140 return false;
141 }
142};
143
Jim Grosbachc466b932010-11-11 18:04:49 +0000144static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000145 switch (Kind) {
146 default: llvm_unreachable("Unknown fixup kind!");
Jim Grosbachc466b932010-11-11 18:04:49 +0000147 case FK_Data_4: return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000148 case ARM::fixup_arm_pcrel_12: return 2;
149 case ARM::fixup_arm_vfp_pcrel_12: return 1;
Jim Grosbachc466b932010-11-11 18:04:49 +0000150 case ARM::fixup_arm_branch: return 3;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000151 }
152}
153
154static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
155 switch (Kind) {
156 default:
157 llvm_unreachable("Unknown fixup kind!");
158 case FK_Data_4:
Jim Grosbacha9a0dde2010-11-09 17:36:59 +0000159 return Value;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000160 case ARM::fixup_arm_pcrel_12:
161 // ARM PC-relative values are offset by 8.
162 return Value - 8;
Jim Grosbachc466b932010-11-11 18:04:49 +0000163 case ARM::fixup_arm_branch:
Jim Grosbach679cbd32010-11-09 01:37:15 +0000164 case ARM::fixup_arm_vfp_pcrel_12:
Jim Grosbachc466b932010-11-11 18:04:49 +0000165 // These values don't encode the low two bits since they're always zero.
166 // Offset by 8 just as above.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000167 return (Value - 8) >> 2;
168 }
169}
170
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000171void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Jim Grosbachc466b932010-11-11 18:04:49 +0000172 uint64_t Value) const {
173 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000174 Value = adjustFixupValue(Fixup.getKind(), Value);
175
176 assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
177 "Invalid fixup offset!");
178 // For each byte of the fragment that the fixup touches, mask in the
179 // bits from the fixup value.
180 for (unsigned i = 0; i != NumBytes; ++i)
181 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000182}
Jim Grosbachf73fd722010-09-30 03:21:00 +0000183} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000184
185TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
186 const std::string &TT) {
187 switch (Triple(TT).getOS()) {
188 case Triple::Darwin:
189 return new DarwinARMAsmBackend(T);
190 case Triple::MinGW32:
191 case Triple::Cygwin:
192 case Triple::Win32:
193 assert(0 && "Windows not supported on ARM");
194 default:
195 return new ELFARMAsmBackend(T, Triple(TT).getOS());
196 }
197}