blob: 02dad4596a115b764d63c9ef5a8e4908533ea991 [file] [log] [blame]
Chris Lattnerb46443a2010-11-15 08:49:58 +00001//===-- 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
Evan Cheng78c10ee2011-07-25 23:24:55 +000010#include "llvm/MC/MCAsmBackend.h"
Evan Cheng966aeb52011-07-25 19:53:23 +000011#include "MCTargetDesc/PPCMCTargetDesc.h"
12#include "MCTargetDesc/PPCFixupKinds.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000013#include "llvm/MC/MCELFObjectWriter.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000014#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000015#include "llvm/MC/MCSectionMachO.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000016#include "llvm/MC/MCObjectWriter.h"
Jim Grosbachba8297e2011-06-24 23:44:37 +000017#include "llvm/MC/MCValue.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000018#include "llvm/Object/MachOFormat.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000019#include "llvm/Support/ELF.h"
20#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000022using namespace llvm;
23
Roman Divacky2c0d69f2011-08-02 15:51:38 +000024static 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
Chris Lattnerb46443a2010-11-15 08:49:58 +000047namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000048class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000049public:
50 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
51 uint32_t CPUSubtype)
52 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbachba8297e2011-06-24 23:44:37 +000053
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) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000058};
59
Evan Cheng78c10ee2011-07-25 23:24:55 +000060class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar297ed282010-12-16 16:08:43 +000061const Target &TheTarget;
62public:
Evan Cheng78c10ee2011-07-25 23:24:55 +000063 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000064
Daniel Dunbar297ed282010-12-16 16:08:43 +000065 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 };
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000076
Daniel Dunbar297ed282010-12-16 16:08:43 +000077 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000078 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000079
Daniel Dunbar297ed282010-12-16 16:08:43 +000080 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
81 "Invalid kind!");
82 return Infos[Kind - FirstTargetFixupKind];
83 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000084
Jim Grosbachec343382012-01-18 18:52:16 +000085 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +000086 // FIXME.
87 return false;
88 }
Jim Grosbach370b78d2011-12-06 00:47:03 +000089
90 bool fixupNeedsRelaxation(const MCFixup &Fixup,
91 uint64_t Value,
92 const MCInstFragment *DF,
93 const MCAsmLayout &Layout) const {
94 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +000095 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach370b78d2011-12-06 00:47:03 +000096 }
97
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000098
Jim Grosbachec343382012-01-18 18:52:16 +000099 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000100 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +0000101 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar297ed282010-12-16 16:08:43 +0000102 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000103
Jim Grosbachec343382012-01-18 18:52:16 +0000104 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000105 // 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;
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000110 }
111
Daniel Dunbar297ed282010-12-16 16:08:43 +0000112 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};
Chris Lattnerb46443a2010-11-15 08:49:58 +0000119} // end anonymous namespace
120
121
122// FIXME: This should be in a separate file.
123namespace {
124 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +0000125 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000126 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000127
Jim Grosbachec343382012-01-18 18:52:16 +0000128 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +0000129 uint64_t Value) const {
Craig Topperbc219812012-02-07 02:50:20 +0000130 llvm_unreachable("UNIMP");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000131 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000132
Chris Lattnerb46443a2010-11-15 08:49:58 +0000133 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
134 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000135 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);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000141 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000142
Chris Lattnerb46443a2010-11-15 08:49:58 +0000143 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
144 return false;
145 }
146 };
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000147
148 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000149 uint8_t OSABI;
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000150 public:
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000151 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
152 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000153
Jim Grosbachec343382012-01-18 18:52:16 +0000154 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000155 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 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000167
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000168 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
169 bool is64 = getPointerSize() == 8;
Rafael Espindolaf51e95a2011-12-22 18:38:06 +0000170 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000171 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000172
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000173 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
174 return false;
175 }
176 };
177
Chris Lattnerb46443a2010-11-15 08:49:58 +0000178} // end anonymous namespace
179
180
181
182
Evan Cheng78c10ee2011-07-25 23:24:55 +0000183MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
Daniel Dunbar912225e2011-04-19 21:14:45 +0000184 if (Triple(TT).isOSDarwin())
Chris Lattnerb46443a2010-11-15 08:49:58 +0000185 return new DarwinPPCAsmBackend(T);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000186
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000187 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
188 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000189}