Revert r162034, r162035 and r162037.
[oota-llvm.git] / lib / Target / PowerPC / MCTargetDesc / 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 "MCTargetDesc/PPCMCTargetDesc.h"
11 #include "MCTargetDesc/PPCFixupKinds.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCMachObjectWriter.h"
16 #include "llvm/MC/MCSectionMachO.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Object/MachOFormat.h"
20 #include "llvm/Support/ELF.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/TargetRegistry.h"
23 using namespace llvm;
24
25 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
26   switch (Kind) {
27   default:
28     llvm_unreachable("Unknown fixup kind!");
29   case FK_Data_1:
30   case FK_Data_2:
31   case FK_Data_4:
32     return Value;
33   case PPC::fixup_ppc_brcond14:
34     return Value & 0x3ffc;
35   case PPC::fixup_ppc_br24:
36     return Value & 0x3fffffc;
37 #if 0
38   case PPC::fixup_ppc_hi16:
39     return (Value >> 16) & 0xffff;
40 #endif
41   case PPC::fixup_ppc_ha16:
42     return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
43   case PPC::fixup_ppc_lo16:
44     return Value & 0xffff;
45   }
46 }
47
48 namespace {
49 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
50 public:
51   PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
52                       uint32_t CPUSubtype)
53     : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
54
55   void RecordRelocation(MachObjectWriter *Writer,
56                         const MCAssembler &Asm, const MCAsmLayout &Layout,
57                         const MCFragment *Fragment, const MCFixup &Fixup,
58                         MCValue Target, uint64_t &FixedValue) {}
59 };
60
61 class PPCAsmBackend : public MCAsmBackend {
62 const Target &TheTarget;
63 public:
64   PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
65
66   unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
67
68   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
69     const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
70       // name                    offset  bits  flags
71       { "fixup_ppc_br24",        6,      24,   MCFixupKindInfo::FKF_IsPCRel },
72       { "fixup_ppc_brcond14",    16,     14,   MCFixupKindInfo::FKF_IsPCRel },
73       { "fixup_ppc_lo16",        16,     16,   0 },
74       { "fixup_ppc_ha16",        16,     16,   0 },
75       { "fixup_ppc_lo14",        16,     14,   0 }
76     };
77
78     if (Kind < FirstTargetFixupKind)
79       return MCAsmBackend::getFixupKindInfo(Kind);
80
81     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
82            "Invalid kind!");
83     return Infos[Kind - FirstTargetFixupKind];
84   }
85
86   bool mayNeedRelaxation(const MCInst &Inst) const {
87     // FIXME.
88     return false;
89   }
90
91   bool fixupNeedsRelaxation(const MCFixup &Fixup,
92                             uint64_t Value,
93                             const MCInstFragment *DF,
94                             const MCAsmLayout &Layout) const {
95     // FIXME.
96     llvm_unreachable("relaxInstruction() unimplemented");
97   }
98
99
100   void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
101     // FIXME.
102     llvm_unreachable("relaxInstruction() unimplemented");
103   }
104
105   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
106     // FIXME: Zero fill for now. That's not right, but at least will get the
107     // section size right.
108     for (uint64_t i = 0; i != Count; ++i)
109       OW->Write8(0);
110     return true;
111   }
112
113   unsigned getPointerSize() const {
114     StringRef Name = TheTarget.getName();
115     if (Name == "ppc64") return 8;
116     assert(Name == "ppc32" && "Unknown target name!");
117     return 4;
118   }
119 };
120 } // end anonymous namespace
121
122
123 // FIXME: This should be in a separate file.
124 namespace {
125   class DarwinPPCAsmBackend : public PPCAsmBackend {
126   public:
127     DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
128
129     void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
130                     uint64_t Value) const {
131       llvm_unreachable("UNIMP");
132     }
133
134     MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
135       bool is64 = getPointerSize() == 8;
136       return createMachObjectWriter(new PPCMachObjectWriter(
137                                       /*Is64Bit=*/is64,
138                                       (is64 ? object::mach::CTM_PowerPC64 :
139                                        object::mach::CTM_PowerPC),
140                                       object::mach::CSPPC_ALL),
141                                     OS, /*IsLittleEndian=*/false);
142     }
143
144     virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
145       return false;
146     }
147   };
148
149   class ELFPPCAsmBackend : public PPCAsmBackend {
150     uint8_t OSABI;
151   public:
152     ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
153       PPCAsmBackend(T), OSABI(OSABI) { }
154
155     void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
156                     uint64_t Value) const {
157       Value = adjustFixupValue(Fixup.getKind(), Value);
158       if (!Value) return;           // Doesn't change encoding.
159
160       unsigned Offset = Fixup.getOffset();
161
162       // For each byte of the fragment that the fixup touches, mask in the bits from
163       // the fixup value. The Value has been "split up" into the appropriate
164       // bitfields above.
165       for (unsigned i = 0; i != 4; ++i)
166         Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
167     }
168
169     MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
170       bool is64 = getPointerSize() == 8;
171       return createPPCELFObjectWriter(OS, is64, OSABI);
172     }
173
174     virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
175       return false;
176     }
177   };
178
179 } // end anonymous namespace
180
181
182
183
184 MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
185   if (Triple(TT).isOSDarwin())
186     return new DarwinPPCAsmBackend(T);
187
188   uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
189   return new ELFPPCAsmBackend(T, OSABI);
190 }