blob: 9dfc6fce9470b78577803bc3e537dfc41565fd5c [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
10#include "llvm/Target/TargetAsmBackend.h"
11#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"
15#include "llvm/MC/MCObjectFormat.h"
16#include "llvm/MC/MCObjectWriter.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) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000027};
28
Daniel Dunbar297ed282010-12-16 16:08:43 +000029class PPCAsmBackend : public TargetAsmBackend {
30const Target &TheTarget;
31public:
32 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000033
Daniel Dunbar297ed282010-12-16 16:08:43 +000034 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
35
36 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
37 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
38 // name offset bits flags
39 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
40 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
41 { "fixup_ppc_lo16", 16, 16, 0 },
42 { "fixup_ppc_ha16", 16, 16, 0 },
43 { "fixup_ppc_lo14", 16, 14, 0 }
44 };
45
46 if (Kind < FirstTargetFixupKind)
47 return TargetAsmBackend::getFixupKindInfo(Kind);
48
49 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
50 "Invalid kind!");
51 return Infos[Kind - FirstTargetFixupKind];
52 }
53
54 bool MayNeedRelaxation(const MCInst &Inst) const {
55 // FIXME.
56 return false;
57 }
58
59 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
60 // FIXME.
61 assert(0 && "RelaxInstruction() unimplemented");
62 }
63
64 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
65 // FIXME: Zero fill for now. That's not right, but at least will get the
66 // section size right.
67 for (uint64_t i = 0; i != Count; ++i)
68 OW->Write8(0);
69 return true;
70 }
71
72 unsigned getPointerSize() const {
73 StringRef Name = TheTarget.getName();
74 if (Name == "ppc64") return 8;
75 assert(Name == "ppc32" && "Unknown target name!");
76 return 4;
77 }
78};
Chris Lattnerb46443a2010-11-15 08:49:58 +000079} // end anonymous namespace
80
81
82// FIXME: This should be in a separate file.
83namespace {
84 class DarwinPPCAsmBackend : public PPCAsmBackend {
85 MCMachOObjectFormat Format;
86 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +000087 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Chris Lattnerb46443a2010-11-15 08:49:58 +000088
89 virtual const MCObjectFormat &getObjectFormat() const {
90 return Format;
91 }
92
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) {
119 switch (Triple(TT).getOS()) {
120 case Triple::Darwin:
121 return new DarwinPPCAsmBackend(T);
122 default:
123 return 0;
124 }
125}