blob: bf437e3aded7a91e70305284ae57342e9e17404d [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:
87 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) {
88 HasScatteredSymbols = true;
89 }
90
91 virtual const MCObjectFormat &getObjectFormat() const {
92 return Format;
93 }
94
Rafael Espindola179821a2010-12-06 19:08:48 +000095 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +000096 uint64_t Value) const {
97 assert(0 && "UNIMP");
98 }
99
Chris Lattnerb46443a2010-11-15 08:49:58 +0000100 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
101 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000102 return createMachObjectWriter(new PPCMachObjectWriter(
103 /*Is64Bit=*/is64,
104 (is64 ? object::mach::CTM_PowerPC64 :
105 object::mach::CTM_PowerPC),
106 object::mach::CSPPC_ALL),
107 OS, /*IsLittleEndian=*/false);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000108 }
109
110 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
111 return false;
112 }
113 };
114} // end anonymous namespace
115
116
117
118
119TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
120 const std::string &TT) {
121 switch (Triple(TT).getOS()) {
122 case Triple::Darwin:
123 return new DarwinPPCAsmBackend(T);
124 default:
125 return 0;
126 }
127}