MC/Mach-O: Switch to using MachOFormat.h.
[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/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"
25 using namespace llvm;
26
27 namespace {
28 class ARMAsmBackend : public TargetAsmBackend {
29 public:
30   ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
31
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;
37
38   unsigned getPointerSize() const {
39     return 4;
40   }
41 };
42 } // end anonymous namespace
43
44 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
45   // FIXME: Thumb targets, different move constant targets..
46   return false;
47 }
48
49 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
50   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
51   return;
52 }
53
54 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
55 //  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);
63   return true;
64 }
65
66 namespace {
67 // FIXME: This should be in a separate file.
68 // ELF is an ELF of course...
69 class ELFARMAsmBackend : public ARMAsmBackend {
70   MCELFObjectFormat Format;
71
72 public:
73   Triple::OSType OSType;
74   ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
75     : ARMAsmBackend(T), OSType(_OSType) {
76     HasScatteredSymbols = true;
77   }
78
79   virtual const MCObjectFormat &getObjectFormat() const {
80     return Format;
81   }
82
83   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
84                   uint64_t Value) const;
85
86   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
87     return createELFObjectWriter(OS, /*Is64Bit=*/false,
88                                  OSType, ELF::EM_ARM,
89                                  /*IsLittleEndian=*/true,
90                                  /*HasRelocationAddend=*/false);
91   }
92 };
93
94 // Fixme: can we raise this to share code between Darwin and ELF?
95 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
96                                   uint64_t Value) const {
97   assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented");
98 }
99
100 namespace {
101 // FIXME: This should be in a separate file.
102 class DarwinARMAsmBackend : public ARMAsmBackend {
103   MCMachOObjectFormat Format;
104 public:
105   DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
106     HasScatteredSymbols = true;
107   }
108
109   virtual const MCObjectFormat &getObjectFormat() const {
110     return Format;
111   }
112
113   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
114                   uint64_t Value) const;
115
116   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
117     // FIXME: Subtarget info should be derived. Force v7 for now.
118     return createMachObjectWriter(OS, /*Is64Bit=*/false,
119                                   object::mach::CTM_ARM,
120                                   object::mach::CSARM_V7,
121                                   /*IsLittleEndian=*/true);
122   }
123
124   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
125     return false;
126   }
127 };
128 } // end anonymous namespace
129
130 static unsigned getFixupKindNumBytes(unsigned Kind) {
131   switch (Kind) {
132   default: llvm_unreachable("Unknown fixup kind!");
133   case FK_Data_4: return 4;
134   case ARM::fixup_arm_pcrel_12: return 2;
135   case ARM::fixup_arm_vfp_pcrel_12: return 1;
136   case ARM::fixup_arm_branch: return 3;
137   }
138 }
139
140 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
141   switch (Kind) {
142   default:
143     llvm_unreachable("Unknown fixup kind!");
144   case FK_Data_4:
145     return Value;
146   case ARM::fixup_arm_pcrel_12:
147     // ARM PC-relative values are offset by 8.
148     return Value - 8;
149   case ARM::fixup_arm_branch:
150   case ARM::fixup_arm_vfp_pcrel_12:
151     // These values don't encode the low two bits since they're always zero.
152     // Offset by 8 just as above.
153     return (Value - 8) >> 2;
154   }
155 }
156
157 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
158                                      uint64_t Value) const {
159   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
160   Value = adjustFixupValue(Fixup.getKind(), Value);
161
162   assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() &&
163          "Invalid fixup offset!");
164   // For each byte of the fragment that the fixup touches, mask in the
165   // bits from the fixup value.
166   for (unsigned i = 0; i != NumBytes; ++i)
167     DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
168 }
169 } // end anonymous namespace
170
171 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
172                                             const std::string &TT) {
173   switch (Triple(TT).getOS()) {
174   case Triple::Darwin:
175     return new DarwinARMAsmBackend(T);
176   case Triple::MinGW32:
177   case Triple::Cygwin:
178   case Triple::Win32:
179     assert(0 && "Windows not supported on ARM");
180   default:
181     return new ELFARMAsmBackend(T, Triple(TT).getOS());
182   }
183 }