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