blob: c4d4ac9b3eb94eb7896d890c431275a2069da458 [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"
Chris Lattnerb46443a2010-11-15 08:49:58 +000015#include "llvm/MC/MCObjectWriter.h"
Daniel Dunbar36d76a82010-11-27 04:38:36 +000016#include "llvm/Object/MachOFormat.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000017#include "llvm/Target/TargetRegistry.h"
Chris Lattnerb46443a2010-11-15 08:49:58 +000018using namespace llvm;
19
20namespace {
Daniel Dunbarae5abd52010-12-16 16:09:19 +000021class PPCMachObjectWriter : public MCMachObjectTargetWriter {
Daniel Dunbar5d05d972010-12-16 17:21:02 +000022public:
23 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
24 uint32_t CPUSubtype)
25 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
Daniel Dunbarae5abd52010-12-16 16:09:19 +000026};
27
Daniel Dunbar297ed282010-12-16 16:08:43 +000028class PPCAsmBackend : public TargetAsmBackend {
29const Target &TheTarget;
30public:
31 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000032
Daniel Dunbar297ed282010-12-16 16:08:43 +000033 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
34
35 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
36 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
37 // name offset bits flags
38 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
39 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
40 { "fixup_ppc_lo16", 16, 16, 0 },
41 { "fixup_ppc_ha16", 16, 16, 0 },
42 { "fixup_ppc_lo14", 16, 14, 0 }
43 };
44
45 if (Kind < FirstTargetFixupKind)
46 return TargetAsmBackend::getFixupKindInfo(Kind);
47
48 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
49 "Invalid kind!");
50 return Infos[Kind - FirstTargetFixupKind];
51 }
52
53 bool MayNeedRelaxation(const MCInst &Inst) const {
54 // FIXME.
55 return false;
56 }
57
58 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
59 // FIXME.
60 assert(0 && "RelaxInstruction() unimplemented");
61 }
62
63 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
64 // FIXME: Zero fill for now. That's not right, but at least will get the
65 // section size right.
66 for (uint64_t i = 0; i != Count; ++i)
67 OW->Write8(0);
68 return true;
69 }
70
71 unsigned getPointerSize() const {
72 StringRef Name = TheTarget.getName();
73 if (Name == "ppc64") return 8;
74 assert(Name == "ppc32" && "Unknown target name!");
75 return 4;
76 }
77};
Chris Lattnerb46443a2010-11-15 08:49:58 +000078} // end anonymous namespace
79
80
81// FIXME: This should be in a separate file.
82namespace {
83 class DarwinPPCAsmBackend : public PPCAsmBackend {
Chris Lattnerb46443a2010-11-15 08:49:58 +000084 public:
Daniel Dunbar7b62afa2010-12-17 02:06:08 +000085 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
Chris Lattnerb46443a2010-11-15 08:49:58 +000086
Rafael Espindola179821a2010-12-06 19:08:48 +000087 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +000088 uint64_t Value) const {
89 assert(0 && "UNIMP");
90 }
91
Chris Lattnerb46443a2010-11-15 08:49:58 +000092 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
93 bool is64 = getPointerSize() == 8;
Daniel Dunbar5d05d972010-12-16 17:21:02 +000094 return createMachObjectWriter(new PPCMachObjectWriter(
95 /*Is64Bit=*/is64,
96 (is64 ? object::mach::CTM_PowerPC64 :
97 object::mach::CTM_PowerPC),
98 object::mach::CSPPC_ALL),
99 OS, /*IsLittleEndian=*/false);
Chris Lattnerb46443a2010-11-15 08:49:58 +0000100 }
101
102 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
103 return false;
104 }
105 };
106} // end anonymous namespace
107
108
109
110
111TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
112 const std::string &TT) {
113 switch (Triple(TT).getOS()) {
114 case Triple::Darwin:
115 return new DarwinPPCAsmBackend(T);
116 default:
117 return 0;
118 }
119}