blob: d8ab689bfa7add71106ff8e18ece388adf45ce29 [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 {
23};
24
Daniel Dunbar297ed282010-12-16 16:08:43 +000025class PPCAsmBackend : public TargetAsmBackend {
26const Target &TheTarget;
27public:
28 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000029
Daniel Dunbar297ed282010-12-16 16:08:43 +000030 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
31
32 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
33 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
34 // name offset bits flags
35 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
36 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
37 { "fixup_ppc_lo16", 16, 16, 0 },
38 { "fixup_ppc_ha16", 16, 16, 0 },
39 { "fixup_ppc_lo14", 16, 14, 0 }
40 };
41
42 if (Kind < FirstTargetFixupKind)
43 return TargetAsmBackend::getFixupKindInfo(Kind);
44
45 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
46 "Invalid kind!");
47 return Infos[Kind - FirstTargetFixupKind];
48 }
49
50 bool MayNeedRelaxation(const MCInst &Inst) const {
51 // FIXME.
52 return false;
53 }
54
55 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
56 // FIXME.
57 assert(0 && "RelaxInstruction() unimplemented");
58 }
59
60 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
61 // FIXME: Zero fill for now. That's not right, but at least will get the
62 // section size right.
63 for (uint64_t i = 0; i != Count; ++i)
64 OW->Write8(0);
65 return true;
66 }
67
68 unsigned getPointerSize() const {
69 StringRef Name = TheTarget.getName();
70 if (Name == "ppc64") return 8;
71 assert(Name == "ppc32" && "Unknown target name!");
72 return 4;
73 }
74};
Chris Lattnerb46443a2010-11-15 08:49:58 +000075} // end anonymous namespace
76
77
78// FIXME: This should be in a separate file.
79namespace {
80 class DarwinPPCAsmBackend : public PPCAsmBackend {
81 MCMachOObjectFormat Format;
82 public:
83 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) {
84 HasScatteredSymbols = true;
85 }
86
87 virtual const MCObjectFormat &getObjectFormat() const {
88 return Format;
89 }
90
Rafael Espindola179821a2010-12-06 19:08:48 +000091 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +000092 uint64_t Value) const {
93 assert(0 && "UNIMP");
94 }
95
Chris Lattnerb46443a2010-11-15 08:49:58 +000096 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
97 bool is64 = getPointerSize() == 8;
Daniel Dunbarae5abd52010-12-16 16:09:19 +000098 return createMachObjectWriter(new PPCMachObjectWriter,
99 OS, /*Is64Bit=*/is64,
Daniel Dunbar36d76a82010-11-27 04:38:36 +0000100 (is64 ? object::mach::CTM_PowerPC64 :
101 object::mach::CTM_PowerPC),
102 object::mach::CSPPC_ALL,
Chris Lattnerb46443a2010-11-15 08:49:58 +0000103 /*IsLittleEndian=*/false);
104 }
105
106 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
107 return false;
108 }
109 };
110} // end anonymous namespace
111
112
113
114
115TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
116 const std::string &TT) {
117 switch (Triple(TT).getOS()) {
118 case Triple::Darwin:
119 return new DarwinPPCAsmBackend(T);
120 default:
121 return 0;
122 }
123}