MC/Mach-O: Stub out explicit MCMachObjectTargetWriter interface.
[oota-llvm.git] / lib / Target / PowerPC / PPCAsmBackend.cpp
1 //===-- PPCAsmBackend.cpp - PPC 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 "PPC.h"
12 #include "PPCFixupKinds.h"
13 #include "llvm/MC/MCMachObjectWriter.h"
14 #include "llvm/MC/MCSectionMachO.h"
15 #include "llvm/MC/MCObjectFormat.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Target/TargetRegistry.h"
19 using namespace llvm;
20
21 namespace {
22 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
23 };
24
25 class PPCAsmBackend : public TargetAsmBackend {
26 const Target &TheTarget;
27 public:
28   PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
29
30   unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
31
32   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
33     const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
34       // name                    offset  bits  flags
35       { "fixup_ppc_br24",        6,      24,   MCFixupKindInfo::FKF_IsPCRel },
36       { "fixup_ppc_brcond14",    16,     14,   MCFixupKindInfo::FKF_IsPCRel },
37       { "fixup_ppc_lo16",        16,     16,   0 },
38       { "fixup_ppc_ha16",        16,     16,   0 },
39       { "fixup_ppc_lo14",        16,     14,   0 }
40     };
41   
42     if (Kind < FirstTargetFixupKind)
43       return TargetAsmBackend::getFixupKindInfo(Kind);
44   
45     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
46            "Invalid kind!");
47     return Infos[Kind - FirstTargetFixupKind];
48   }
49   
50   bool MayNeedRelaxation(const MCInst &Inst) const {
51     // FIXME.
52     return false;
53   }
54   
55   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
56     // FIXME.
57     assert(0 && "RelaxInstruction() unimplemented");
58   }
59   
60   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
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);
65     return true;
66   }      
67   
68   unsigned getPointerSize() const {
69     StringRef Name = TheTarget.getName();
70     if (Name == "ppc64") return 8;
71     assert(Name == "ppc32" && "Unknown target name!");
72     return 4;
73   }
74 };
75 } // end anonymous namespace
76
77
78 // FIXME: This should be in a separate file.
79 namespace {
80   class DarwinPPCAsmBackend : public PPCAsmBackend {
81     MCMachOObjectFormat Format;
82   public:
83     DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) {
84       HasScatteredSymbols = true;
85     }
86     
87     virtual const MCObjectFormat &getObjectFormat() const {
88       return Format;
89     }
90     
91     void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
92                     uint64_t Value) const {
93       assert(0 && "UNIMP");
94     }
95     
96     MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
97       bool is64 = getPointerSize() == 8;
98       return createMachObjectWriter(new PPCMachObjectWriter,
99                                     OS, /*Is64Bit=*/is64,
100                                     (is64 ? object::mach::CTM_PowerPC64 :
101                                      object::mach::CTM_PowerPC),
102                                     object::mach::CSPPC_ALL,
103                                     /*IsLittleEndian=*/false);
104     }
105     
106     virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
107       return false;
108     }
109   };
110 } // end anonymous namespace
111
112
113
114
115 TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
116                                             const std::string &TT) {
117   switch (Triple(TT).getOS()) {
118   case Triple::Darwin:
119     return new DarwinPPCAsmBackend(T);
120   default:
121     return 0;
122   }
123 }