blob: 75a5511e6cc6fae1d6d4ef83ce638189be5d574a [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"
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000025using namespace llvm;
26
27namespace {
28class ARMAsmBackend : public TargetAsmBackend {
29public:
Chris Lattnerb75c6512010-11-17 05:41:32 +000030 ARMAsmBackend(const Target &T) : TargetAsmBackend(T) {}
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// if ((Count % 4) != 0) {
56// // Fixme: % 2 for Thumb?
57// return false;
58// }
59 // FIXME: Zero fill for now. That's not right, but at least will get the
60 // section size right.
61 for (uint64_t i = 0; i != Count; ++i)
62 OW->Write8(0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000063 return true;
Jim Grosbach87dc3aa2010-09-30 03:20:34 +000064}
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000065
66namespace {
67// FIXME: This should be in a separate file.
68// ELF is an ELF of course...
69class ELFARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +000070 MCELFObjectFormat Format;
71
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000072public:
73 Triple::OSType OSType;
74 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
75 : ARMAsmBackend(T), OSType(_OSType) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000076 HasScatteredSymbols = true;
77 }
78
Rafael Espindolaf230df92010-10-16 18:23:53 +000079 virtual const MCObjectFormat &getObjectFormat() const {
80 return Format;
81 }
82
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000083 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
84 uint64_t Value) const;
85
86 bool isVirtualSection(const MCSection &Section) const {
87 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
88 return SE.getType() == MCSectionELF::SHT_NOBITS;
89 }
90
91 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000092 return createELFObjectWriter(OS, /*Is64Bit=*/false,
93 OSType, ELF::EM_ARM,
94 /*IsLittleEndian=*/true,
95 /*HasRelocationAddend=*/false);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +000096 }
97};
98
Jason W Kima4c27242010-09-30 14:58:19 +000099// Fixme: can we raise this to share code between Darwin and ELF?
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000100void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
101 uint64_t Value) const {
102 assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented");
103}
104
Chris Lattnerb75c6512010-11-17 05:41:32 +0000105namespace {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000106// FIXME: This should be in a separate file.
107class DarwinARMAsmBackend : public ARMAsmBackend {
Rafael Espindolaf230df92010-10-16 18:23:53 +0000108 MCMachOObjectFormat Format;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000109public:
Chris Lattnerb75c6512010-11-17 05:41:32 +0000110 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000111 HasScatteredSymbols = true;
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000112 }
113
Rafael Espindolaf230df92010-10-16 18:23:53 +0000114 virtual const MCObjectFormat &getObjectFormat() const {
115 return Format;
116 }
117
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000118 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
119 uint64_t Value) const;
120
121 bool isVirtualSection(const MCSection &Section) const {
122 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
123 return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
124 SMO.getType() == MCSectionMachO::S_GB_ZEROFILL ||
125 SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
126 }
127
128 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jim Grosbachc9d14392010-11-05 18:48:58 +0000129 // FIXME: Subtarget info should be derived. Force v7 for now.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000130 return createMachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeARM,
131 MachO::CPUSubType_ARM_V7,
132 /*IsLittleEndian=*/true);
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000133 }
134
135 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
136 return false;
137 }
138};
Chris Lattnerb75c6512010-11-17 05:41:32 +0000139} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000140
Jim Grosbachc466b932010-11-11 18:04:49 +0000141static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000142 switch (Kind) {
143 default: llvm_unreachable("Unknown fixup kind!");
Jim Grosbachc466b932010-11-11 18:04:49 +0000144 case FK_Data_4: return 4;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000145 case ARM::fixup_arm_pcrel_12: return 2;
146 case ARM::fixup_arm_vfp_pcrel_12: return 1;
Jim Grosbachc466b932010-11-11 18:04:49 +0000147 case ARM::fixup_arm_branch: return 3;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000148 }
149}
150
151static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
152 switch (Kind) {
153 default:
154 llvm_unreachable("Unknown fixup kind!");
155 case FK_Data_4:
Jim Grosbacha9a0dde2010-11-09 17:36:59 +0000156 return Value;
Jim Grosbach679cbd32010-11-09 01:37:15 +0000157 case ARM::fixup_arm_pcrel_12:
158 // ARM PC-relative values are offset by 8.
159 return Value - 8;
Jim Grosbachc466b932010-11-11 18:04:49 +0000160 case ARM::fixup_arm_branch:
Jim Grosbach679cbd32010-11-09 01:37:15 +0000161 case ARM::fixup_arm_vfp_pcrel_12:
Jim Grosbachc466b932010-11-11 18:04:49 +0000162 // These values don't encode the low two bits since they're always zero.
163 // Offset by 8 just as above.
Jim Grosbach679cbd32010-11-09 01:37:15 +0000164 return (Value - 8) >> 2;
165 }
166}
167
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000168void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
Jim Grosbachc466b932010-11-11 18:04:49 +0000169 uint64_t Value) const {
170 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbach679cbd32010-11-09 01:37:15 +0000171 Value = adjustFixupValue(Fixup.getKind(), Value);
172
173 assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
174 "Invalid fixup offset!");
175 // For each byte of the fragment that the fixup touches, mask in the
176 // bits from the fixup value.
177 for (unsigned i = 0; i != NumBytes; ++i)
178 DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000179}
Jim Grosbachf73fd722010-09-30 03:21:00 +0000180} // end anonymous namespace
Jason W Kimd4d4f4f2010-09-30 02:17:26 +0000181
182TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
183 const std::string &TT) {
184 switch (Triple(TT).getOS()) {
185 case Triple::Darwin:
186 return new DarwinARMAsmBackend(T);
187 case Triple::MinGW32:
188 case Triple::Cygwin:
189 case Triple::Win32:
190 assert(0 && "Windows not supported on ARM");
191 default:
192 return new ELFARMAsmBackend(T, Triple(TT).getOS());
193 }
194}