Add support for a few simple fixups to the ARM Darwin asm backend. This allows
[oota-llvm.git] / lib / Target / ARM / ARMAsmBackend.cpp
1 //===-- 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"
12 #include "ARMFixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/ELFObjectWriter.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectFormat.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MachObjectWriter.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/MachO.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetRegistry.h"
27 #include "llvm/Target/TargetAsmBackend.h"
28 using namespace llvm;
29
30 namespace {
31 class ARMAsmBackend : public TargetAsmBackend {
32 public:
33   ARMAsmBackend(const Target &T)
34     : TargetAsmBackend(T) {
35   }
36
37   bool MayNeedRelaxation(const MCInst &Inst) const;
38
39   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
40
41   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
42
43   unsigned getPointerSize() const {
44     return 4;
45   }
46 };
47
48 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
49   // FIXME: Thumb targets, different move constant targets..
50   return false;
51 }
52
53 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
54   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
55   return;
56 }
57
58 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
59   if ((Count % 4) != 0) {
60     // Fixme: % 2 for Thumb?
61     return false;
62   }
63   return true;
64 }
65 } // end anonymous namespace
66
67 namespace {
68 // FIXME: This should be in a separate file.
69 // ELF is an ELF of course...
70 class ELFARMAsmBackend : public ARMAsmBackend {
71   MCELFObjectFormat Format;
72
73 public:
74   Triple::OSType OSType;
75   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
76     : ARMAsmBackend(T), OSType(_OSType) {
77     HasScatteredSymbols = true;
78   }
79
80   virtual const MCObjectFormat &getObjectFormat() const {
81     return Format;
82   }
83
84   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
85                   uint64_t Value) const;
86
87   bool isVirtualSection(const MCSection &Section) const {
88     const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
89     return SE.getType() == MCSectionELF::SHT_NOBITS;
90   }
91
92   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
93     return new ELFObjectWriter(OS, /*Is64Bit=*/false,
94                                OSType, ELF::EM_ARM,
95                                /*IsLittleEndian=*/true,
96                                /*HasRelocationAddend=*/false);
97   }
98 };
99
100 // Fixme: can we raise this to share code between Darwin and ELF?
101 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
102                                   uint64_t Value) const {
103   assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented");
104 }
105
106 // FIXME: This should be in a separate file.
107 class DarwinARMAsmBackend : public ARMAsmBackend {
108   MCMachOObjectFormat Format;
109
110 public:
111   DarwinARMAsmBackend(const Target &T)
112     : ARMAsmBackend(T) {
113     HasScatteredSymbols = true;
114   }
115
116   virtual const MCObjectFormat &getObjectFormat() const {
117     return Format;
118   }
119
120   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
121                   uint64_t Value) const;
122
123   bool isVirtualSection(const MCSection &Section) const {
124     const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
125     return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
126             SMO.getType() == MCSectionMachO::S_GB_ZEROFILL ||
127             SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
128   }
129
130   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
131     // FIXME: Subtarget info should be derived. Force v7 for now.
132     return new MachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeARM,
133                                 MachO::CPUSubType_ARM_V7);
134   }
135
136   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
137     return false;
138   }
139 };
140
141 static unsigned getFixupKindLog2Size(unsigned Kind) {
142   switch (Kind) {
143   default: llvm_unreachable("Unknown fixup kind!");
144   case FK_Data_4: return 2;
145   case ARM::fixup_arm_pcrel_12: return 2;
146   case ARM::fixup_arm_vfp_pcrel_12: return 1;
147   }
148 }
149
150 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
151   switch (Kind) {
152   default:
153     llvm_unreachable("Unknown fixup kind!");
154   case FK_Data_4:
155   case ARM::fixup_arm_pcrel_12:
156     // ARM PC-relative values are offset by 8.
157     return Value - 8;
158   case ARM::fixup_arm_vfp_pcrel_12:
159     // The VFP ld/st immediate value doesn't encode the low two bits since
160     // they're always zero. Offset by 8 just as above.
161     return (Value - 8) >> 2;
162   }
163 }
164
165 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
166                                   uint64_t Value) const {
167   unsigned NumBytes = getFixupKindLog2Size(Fixup.getKind());
168   Value = adjustFixupValue(Fixup.getKind(), Value);
169
170   assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
171          "Invalid fixup offset!");
172   // For each byte of the fragment that the fixup touches, mask in the
173   // bits from the fixup value.
174   for (unsigned i = 0; i != NumBytes; ++i)
175     DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
176 }
177 } // end anonymous namespace
178
179 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
180                                             const std::string &TT) {
181   switch (Triple(TT).getOS()) {
182   case Triple::Darwin:
183     return new DarwinARMAsmBackend(T);
184   case Triple::MinGW32:
185   case Triple::Cygwin:
186   case Triple::Win32:
187     assert(0 && "Windows not supported on ARM");
188   default:
189     return new ELFARMAsmBackend(T, Triple(TT).getOS());
190   }
191 }