blob: 87ecb13a4c763d33d8ca1d3619d9cf17f13747a3 [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 Cheng966aeb52011-07-25 19:53:23 +000010#include "MCTargetDesc/PPCMCTargetDesc.h"
11#include "MCTargetDesc/PPCFixupKinds.h"
Craig Topper79aa3412012-03-17 18:46:09 +000012#include "llvm/MC/MCAsmBackend.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000013#include "llvm/MC/MCELFObjectWriter.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000014#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000015#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000016#include "llvm/MC/MCSectionMachO.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000017#include "llvm/MC/MCObjectWriter.h"
Jim Grosbachba8297e2011-06-24 23:44:37 +000018#include "llvm/MC/MCValue.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000019#include "llvm/Object/MachOFormat.h"
Roman Divacky2c0d69f2011-08-02 15:51:38 +000020#include "llvm/Support/ELF.h"
21#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000023using namespace llvm;
24
Roman Divacky2c0d69f2011-08-02 15:51:38 +000025static 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:
Adhemerval Zanellaaa714282012-10-25 12:27:42 +000032 case FK_Data_8:
33 case PPC::fixup_ppc_toc:
Roman Divacky2c0d69f2011-08-02 15:51:38 +000034 return Value;
Adhemerval Zanellaaa714282012-10-25 12:27:42 +000035 case PPC::fixup_ppc_lo14:
36 case PPC::fixup_ppc_toc16_ds:
37 return (Value & 0xffff) << 2;
Roman Divacky2c0d69f2011-08-02 15:51:38 +000038 case PPC::fixup_ppc_brcond14:
Adhemerval Zanellaaa714282012-10-25 12:27:42 +000039 return Value & 0xfffc;
Roman Divacky2c0d69f2011-08-02 15:51:38 +000040 case PPC::fixup_ppc_br24:
41 return Value & 0x3fffffc;
42#if 0
43 case PPC::fixup_ppc_hi16:
44 return (Value >> 16) & 0xffff;
45#endif
46 case PPC::fixup_ppc_ha16:
47 return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
48 case PPC::fixup_ppc_lo16:
Adhemerval Zanellaaa714282012-10-25 12:27:42 +000049 case PPC::fixup_ppc_toc16:
Roman Divacky2c0d69f2011-08-02 15:51:38 +000050 return Value & 0xffff;
51 }
52}
53
Chris Lattnerb46443a2010-11-15 08:49:58 +000054namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000055class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000056public:
57 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
58 uint32_t CPUSubtype)
59 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbachba8297e2011-06-24 23:44:37 +000060
61 void RecordRelocation(MachObjectWriter *Writer,
62 const MCAssembler &Asm, const MCAsmLayout &Layout,
63 const MCFragment *Fragment, const MCFixup &Fixup,
64 MCValue Target, uint64_t &FixedValue) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000065};
66
Evan Cheng78c10ee2011-07-25 23:24:55 +000067class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar297ed282010-12-16 16:08:43 +000068const Target &TheTarget;
69public:
Evan Cheng78c10ee2011-07-25 23:24:55 +000070 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000071
Daniel Dunbar297ed282010-12-16 16:08:43 +000072 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
73
74 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
75 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
76 // name offset bits flags
77 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_ppc_lo16", 16, 16, 0 },
80 { "fixup_ppc_ha16", 16, 16, 0 },
Adhemerval Zanellaaa714282012-10-25 12:27:42 +000081 { "fixup_ppc_lo14", 16, 14, 0 },
82 { "fixup_ppc_toc", 0, 64, 0 },
83 { "fixup_ppc_toc16", 16, 16, 0 },
84 { "fixup_ppc_toc16_ds", 16, 14, 0 }
Daniel Dunbar297ed282010-12-16 16:08:43 +000085 };
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000086
Daniel Dunbar297ed282010-12-16 16:08:43 +000087 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000088 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000089
Daniel Dunbar297ed282010-12-16 16:08:43 +000090 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
91 "Invalid kind!");
92 return Infos[Kind - FirstTargetFixupKind];
93 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000094
Jim Grosbachec343382012-01-18 18:52:16 +000095 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +000096 // FIXME.
97 return false;
98 }
Jim Grosbach370b78d2011-12-06 00:47:03 +000099
100 bool fixupNeedsRelaxation(const MCFixup &Fixup,
101 uint64_t Value,
102 const MCInstFragment *DF,
103 const MCAsmLayout &Layout) const {
104 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +0000105 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach370b78d2011-12-06 00:47:03 +0000106 }
107
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000108
Jim Grosbachec343382012-01-18 18:52:16 +0000109 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000110 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +0000111 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar297ed282010-12-16 16:08:43 +0000112 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000113
Jim Grosbachec343382012-01-18 18:52:16 +0000114 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000115 // FIXME: Zero fill for now. That's not right, but at least will get the
116 // section size right.
117 for (uint64_t i = 0; i != Count; ++i)
118 OW->Write8(0);
119 return true;
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000120 }
121
Daniel Dunbar297ed282010-12-16 16:08:43 +0000122 unsigned getPointerSize() const {
123 StringRef Name = TheTarget.getName();
124 if (Name == "ppc64") return 8;
125 assert(Name == "ppc32" && "Unknown target name!");
126 return 4;
127 }
128};
Chris Lattnerb46443a2010-11-15 08:49:58 +0000129} // end anonymous namespace
130
131
132// FIXME: This should be in a separate file.
133namespace {
134 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +0000135 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000136 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000137
Jim Grosbachec343382012-01-18 18:52:16 +0000138 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +0000139 uint64_t Value) const {
Craig Topperbc219812012-02-07 02:50:20 +0000140 llvm_unreachable("UNIMP");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000141 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000142
Chris Lattnerb46443a2010-11-15 08:49:58 +0000143 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
144 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000145 return createMachObjectWriter(new PPCMachObjectWriter(
146 /*Is64Bit=*/is64,
147 (is64 ? object::mach::CTM_PowerPC64 :
148 object::mach::CTM_PowerPC),
149 object::mach::CSPPC_ALL),
150 OS, /*IsLittleEndian=*/false);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000151 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000152
Chris Lattnerb46443a2010-11-15 08:49:58 +0000153 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
154 return false;
155 }
156 };
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000157
158 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000159 uint8_t OSABI;
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000160 public:
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000161 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
162 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000163
Jim Grosbachec343382012-01-18 18:52:16 +0000164 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000165 uint64_t Value) const {
166 Value = adjustFixupValue(Fixup.getKind(), Value);
167 if (!Value) return; // Doesn't change encoding.
168
169 unsigned Offset = Fixup.getOffset();
170
171 // For each byte of the fragment that the fixup touches, mask in the bits from
172 // the fixup value. The Value has been "split up" into the appropriate
173 // bitfields above.
174 for (unsigned i = 0; i != 4; ++i)
175 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
176 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000177
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000178 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
179 bool is64 = getPointerSize() == 8;
Rafael Espindolaf51e95a2011-12-22 18:38:06 +0000180 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000181 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000182
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000183 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
184 return false;
185 }
186 };
187
Chris Lattnerb46443a2010-11-15 08:49:58 +0000188} // end anonymous namespace
189
190
191
192
Roman Divacky536a88a2012-09-18 16:08:49 +0000193MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) {
Daniel Dunbar912225e2011-04-19 21:14:45 +0000194 if (Triple(TT).isOSDarwin())
Chris Lattnerb46443a2010-11-15 08:49:58 +0000195 return new DarwinPPCAsmBackend(T);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000196
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000197 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
198 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000199}