blob: 8bfacb6aa659282b95c71977442355528b88d103 [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 Chenga7cfc082011-07-23 00:45:41 +000010#include "llvm/MC/TargetAsmBackend.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000011#include "PPC.h"
12#include "PPCFixupKinds.h"
Daniel Dunbaraa4b7dd2010-12-16 16:08:33 +000013#include "llvm/MC/MCMachObjectWriter.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000014#include "llvm/MC/MCSectionMachO.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000015#include "llvm/MC/MCObjectWriter.h"
Jim Grosbachba8297e2011-06-24 23:44:37 +000016#include "llvm/MC/MCValue.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000017#include "llvm/Object/MachOFormat.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000018#include "llvm/Target/TargetRegistry.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000019using namespace llvm;
20
21namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000022class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000023public:
24 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
25 uint32_t CPUSubtype)
26 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Jim Grosbachba8297e2011-06-24 23:44:37 +000027
28 void RecordRelocation(MachObjectWriter *Writer,
29 const MCAssembler &Asm, const MCAsmLayout &Layout,
30 const MCFragment *Fragment, const MCFixup &Fixup,
31 MCValue Target, uint64_t &FixedValue) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000032};
33
Daniel Dunbar297ed282010-12-16 16:08:43 +000034class PPCAsmBackend : public TargetAsmBackend {
35const Target &TheTarget;
36public:
37 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000038
Daniel Dunbar297ed282010-12-16 16:08:43 +000039 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
40
41 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
42 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
43 // name offset bits flags
44 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
45 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
46 { "fixup_ppc_lo16", 16, 16, 0 },
47 { "fixup_ppc_ha16", 16, 16, 0 },
48 { "fixup_ppc_lo14", 16, 14, 0 }
49 };
50
51 if (Kind < FirstTargetFixupKind)
52 return TargetAsmBackend::getFixupKindInfo(Kind);
53
54 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
55 "Invalid kind!");
56 return Infos[Kind - FirstTargetFixupKind];
57 }
58
59 bool MayNeedRelaxation(const MCInst &Inst) const {
60 // FIXME.
61 return false;
62 }
63
64 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 // FIXME.
66 assert(0 && "RelaxInstruction() unimplemented");
67 }
68
69 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
70 // FIXME: Zero fill for now. That's not right, but at least will get the
71 // section size right.
72 for (uint64_t i = 0; i != Count; ++i)
73 OW->Write8(0);
74 return true;
75 }
76
77 unsigned getPointerSize() const {
78 StringRef Name = TheTarget.getName();
79 if (Name == "ppc64") return 8;
80 assert(Name == "ppc32" && "Unknown target name!");
81 return 4;
82 }
83};
Chris Lattnerb46443a2010-11-15 08:49:58 +000084} // end anonymous namespace
85
86
87// FIXME: This should be in a separate file.
88namespace {
89 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +000090 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +000091 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Chris Lattnerb46443a2010-11-15 08:49:58 +000092
Rafael Espindola179821a2010-12-06 19:08:48 +000093 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +000094 uint64_t Value) const {
95 assert(0 && "UNIMP");
96 }
97
Chris Lattnerb46443a2010-11-15 08:49:58 +000098 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
99 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000100 return createMachObjectWriter(new PPCMachObjectWriter(
101 /*Is64Bit=*/is64,
102 (is64 ? object::mach::CTM_PowerPC64 :
103 object::mach::CTM_PowerPC),
104 object::mach::CSPPC_ALL),
105 OS, /*IsLittleEndian=*/false);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000106 }
107
108 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
109 return false;
110 }
111 };
112} // end anonymous namespace
113
114
115
116
117TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
118 const std::string &TT) {
Daniel Dunbar912225e2011-04-19 21:14:45 +0000119 if (Triple(TT).isOSDarwin())
Chris Lattnerb46443a2010-11-15 08:49:58 +0000120 return new DarwinPPCAsmBackend(T);
Daniel Dunbar912225e2011-04-19 21:14:45 +0000121
122 return 0;
Chris Lattnerb46443a2010-11-15 08:49:58 +0000123}