blob: 48de583afdf1207d6316677b514734b759d739f5 [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:
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
Chris Lattnerb46443a2010-11-15 08:49:58 +000048namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000049class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000050public:
51 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
52 uint32_t CPUSubtype)
53 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbachba8297e2011-06-24 23:44:37 +000054
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) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000059};
60
Evan Cheng78c10ee2011-07-25 23:24:55 +000061class PPCAsmBackend : public MCAsmBackend {
Daniel Dunbar297ed282010-12-16 16:08:43 +000062const Target &TheTarget;
63public:
Evan Cheng78c10ee2011-07-25 23:24:55 +000064 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000065
Daniel Dunbar297ed282010-12-16 16:08:43 +000066 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 };
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000077
Daniel Dunbar297ed282010-12-16 16:08:43 +000078 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000079 return MCAsmBackend::getFixupKindInfo(Kind);
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000080
Daniel Dunbar297ed282010-12-16 16:08:43 +000081 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
82 "Invalid kind!");
83 return Infos[Kind - FirstTargetFixupKind];
84 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000085
Jim Grosbachec343382012-01-18 18:52:16 +000086 bool mayNeedRelaxation(const MCInst &Inst) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +000087 // FIXME.
88 return false;
89 }
Jim Grosbach370b78d2011-12-06 00:47:03 +000090
91 bool fixupNeedsRelaxation(const MCFixup &Fixup,
92 uint64_t Value,
93 const MCInstFragment *DF,
94 const MCAsmLayout &Layout) const {
95 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +000096 llvm_unreachable("relaxInstruction() unimplemented");
Jim Grosbach370b78d2011-12-06 00:47:03 +000097 }
98
Jim Grosbachbc3af9b2012-01-18 18:52:20 +000099
Jim Grosbachec343382012-01-18 18:52:16 +0000100 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000101 // FIXME.
Craig Topperbc219812012-02-07 02:50:20 +0000102 llvm_unreachable("relaxInstruction() unimplemented");
Daniel Dunbar297ed282010-12-16 16:08:43 +0000103 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000104
Jim Grosbachec343382012-01-18 18:52:16 +0000105 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Daniel Dunbar297ed282010-12-16 16:08:43 +0000106 // 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;
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000111 }
112
Daniel Dunbar297ed282010-12-16 16:08:43 +0000113 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};
Chris Lattnerb46443a2010-11-15 08:49:58 +0000120} // end anonymous namespace
121
122
123// FIXME: This should be in a separate file.
124namespace {
125 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +0000126 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +0000127 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000128
Jim Grosbachec343382012-01-18 18:52:16 +0000129 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +0000130 uint64_t Value) const {
Craig Topperbc219812012-02-07 02:50:20 +0000131 llvm_unreachable("UNIMP");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000132 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000133
Chris Lattnerb46443a2010-11-15 08:49:58 +0000134 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
135 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000136 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);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000142 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000143
Chris Lattnerb46443a2010-11-15 08:49:58 +0000144 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
145 return false;
146 }
147 };
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000148
149 class ELFPPCAsmBackend : public PPCAsmBackend {
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000150 uint8_t OSABI;
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000151 public:
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000152 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
153 PPCAsmBackend(T), OSABI(OSABI) { }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000154
Jim Grosbachec343382012-01-18 18:52:16 +0000155 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000156 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 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000168
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000169 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
170 bool is64 = getPointerSize() == 8;
Rafael Espindolaf51e95a2011-12-22 18:38:06 +0000171 return createPPCELFObjectWriter(OS, is64, OSABI);
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000172 }
Jim Grosbachbc3af9b2012-01-18 18:52:20 +0000173
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000174 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
175 return false;
176 }
177 };
178
Chris Lattnerb46443a2010-11-15 08:49:58 +0000179} // end anonymous namespace
180
181
182
183
Evan Cheng78c10ee2011-07-25 23:24:55 +0000184MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
Daniel Dunbar912225e2011-04-19 21:14:45 +0000185 if (Triple(TT).isOSDarwin())
Chris Lattnerb46443a2010-11-15 08:49:58 +0000186 return new DarwinPPCAsmBackend(T);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000187
Rafael Espindoladc9a8a32011-12-21 17:00:36 +0000188 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
189 return new ELFPPCAsmBackend(T, OSABI);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000190}