Add a MCObjectFormat class so that code common to all targets that use a
[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 //FIXME: add #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/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetRegistry.h"
25 #include "llvm/Target/TargetAsmBackend.h"
26 using namespace llvm;
27
28 namespace {
29 class ARMAsmBackend : public TargetAsmBackend {
30 public:
31   ARMAsmBackend(const Target &T)
32     : TargetAsmBackend(T) {
33   }
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;
40
41   unsigned getPointerSize() const {
42     return 4;
43   }
44 };
45
46 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
47   // FIXME: Thumb targets, different move constant targets..
48   return false;
49 }
50
51 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
52   assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
53   return;
54 }
55
56 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
57   assert(0 && "ARMAsmBackend::WriteNopData() unimplemented");
58   if ((Count % 4) != 0) {
59     // Fixme: % 2 for Thumb?
60     return false;
61   }
62   return false;
63 }
64 } // end anonymous namespace
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   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 {
92     return new ELFObjectWriter(OS, /*Is64Bit=*/false,
93                                OSType,
94                                /*IsLittleEndian=*/true,
95                                /*HasRelocationAddend=*/false);
96   }
97 };
98
99 // Fixme: can we raise this to share code between Darwin and ELF?
100 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
101                                   uint64_t Value) const {
102   assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented");
103 }
104
105 // FIXME: This should be in a separate file.
106 class DarwinARMAsmBackend : public ARMAsmBackend {
107   MCMachOObjectFormat Format;
108
109 public:
110   DarwinARMAsmBackend(const Target &T)
111     : ARMAsmBackend(T) {
112     HasScatteredSymbols = true;
113     assert(0 && "DarwinARMAsmBackend::DarwinARMAsmBackend() unimplemented");
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     return new MachObjectWriter(OS, /*Is64Bit=*/false);
132   }
133
134   virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
135     return false;
136   }
137 };
138
139 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
140                                   uint64_t Value) const {
141   assert(0 && "DarwinARMAsmBackend::ApplyFixup() unimplemented");
142 }
143 } // end anonymous namespace
144
145 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
146                                             const std::string &TT) {
147   switch (Triple(TT).getOS()) {
148   case Triple::Darwin:
149     return new DarwinARMAsmBackend(T);
150   case Triple::MinGW32:
151   case Triple::Cygwin:
152   case Triple::Win32:
153     assert(0 && "Windows not supported on ARM");
154   default:
155     return new ELFARMAsmBackend(T, Triple(TT).getOS());
156   }
157 }