blob: 75366196e63d979dfccdba5cdf12f46c4ea4cbcb [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 };
76
77 if (Kind < FirstTargetFixupKind)
Evan Cheng78c10ee2011-07-25 23:24:55 +000078 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar297ed282010-12-16 16:08:43 +000079
80 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
81 "Invalid kind!");
82 return Infos[Kind - FirstTargetFixupKind];
83 }
84
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.
Jim Grosbachec343382012-01-18 18:52:16 +000095 assert(0 && "relaxInstruction() unimplemented");
NAKAMURA Takumi6482e912011-12-06 01:48:32 +000096 return false;
Jim Grosbach370b78d2011-12-06 00:47:03 +000097 }
98
Daniel Dunbar297ed282010-12-16 16:08:43 +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.
Jim Grosbachec343382012-01-18 18:52:16 +0000102 assert(0 && "relaxInstruction() unimplemented");
Daniel Dunbar297ed282010-12-16 16:08:43 +0000103 }
104
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;
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};
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) { }
Chris Lattnerb46443a2010-11-15 08:49:58 +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 {
131 assert(0 && "UNIMP");
132 }
133
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 }
143
144 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) { }
Roman Divacky2c0d69f2011-08-02 15:51:38 +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 }
168
169 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 }
173
174 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}