blob: c8cd736f1b642666f3357bd80ba210c386ec97ce [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 {
22 class PPCAsmBackend : public TargetAsmBackend {
Rafael Espindolafd467972010-11-26 04:24:21 +000023 const Target &TheTarget;
Chris Lattnerb46443a2010-11-15 08:49:58 +000024 public:
Rafael Espindolafd467972010-11-26 04:24:21 +000025 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
Daniel Dunbar2761fc42010-12-16 03:20:06 +000026
27 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
28
29 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
30 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
31 // name offset bits flags
32 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
33 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
34 { "fixup_ppc_lo16", 16, 16, 0 },
35 { "fixup_ppc_ha16", 16, 16, 0 },
36 { "fixup_ppc_lo14", 16, 14, 0 }
37 };
38
39 if (Kind < FirstTargetFixupKind)
40 return TargetAsmBackend::getFixupKindInfo(Kind);
41
42 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
43 "Invalid kind!");
44 return Infos[Kind - FirstTargetFixupKind];
45 }
Chris Lattnerb46443a2010-11-15 08:49:58 +000046
47 bool MayNeedRelaxation(const MCInst &Inst) const {
48 // FIXME.
49 return false;
50 }
51
52 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
53 // FIXME.
54 assert(0 && "RelaxInstruction() unimplemented");
55 }
56
57 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
58 // FIXME: Zero fill for now. That's not right, but at least will get the
59 // section size right.
60 for (uint64_t i = 0; i != Count; ++i)
61 OW->Write8(0);
62 return true;
63 }
64
65 unsigned getPointerSize() const {
66 StringRef Name = TheTarget.getName();
67 if (Name == "ppc64") return 8;
68 assert(Name == "ppc32" && "Unknown target name!");
69 return 4;
70 }
71 };
72} // end anonymous namespace
73
74
75// FIXME: This should be in a separate file.
76namespace {
77 class DarwinPPCAsmBackend : public PPCAsmBackend {
78 MCMachOObjectFormat Format;
79 public:
80 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) {
81 HasScatteredSymbols = true;
82 }
83
84 virtual const MCObjectFormat &getObjectFormat() const {
85 return Format;
86 }
87
Rafael Espindola179821a2010-12-06 19:08:48 +000088 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Chris Lattnerb46443a2010-11-15 08:49:58 +000089 uint64_t Value) const {
90 assert(0 && "UNIMP");
91 }
92
Chris Lattnerb46443a2010-11-15 08:49:58 +000093 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
94 bool is64 = getPointerSize() == 8;
95 return createMachObjectWriter(OS, /*Is64Bit=*/is64,
Daniel Dunbar36d76a82010-11-27 04:38:36 +000096 (is64 ? object::mach::CTM_PowerPC64 :
97 object::mach::CTM_PowerPC),
98 object::mach::CSPPC_ALL,
Chris Lattnerb46443a2010-11-15 08:49:58 +000099 /*IsLittleEndian=*/false);
100 }
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}