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