Skip to content

Commit aac9fa7

Browse files
committed
Wire up primitive support in the assembler backend for writing .o files
directly on the mac. This is very early, doesn't support relocations and has a terrible hack to avoid .machine from being printed, but despite that it generates an bitwise-identical-to-cctools .o file for stuff like this: define i32 @test() nounwind { ret i32 42 } I don't plan to continue pushing this forward, but if anyone else was interested in doing it, it should be really straight-forward. llvm-svn: 119136
1 parent 5d30c69 commit aac9fa7

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed

llvm/lib/Target/PowerPC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tablegen(PPCGenCallingConv.inc -gen-callingconv)
1313
tablegen(PPCGenSubtarget.inc -gen-subtarget)
1414

1515
add_llvm_target(PowerPCCodeGen
16+
PPCAsmBackend.cpp
1617
PPCAsmPrinter.cpp
1718
PPCBranchSelector.cpp
1819
PPCCodeEmitter.cpp

llvm/lib/Target/PowerPC/PPC.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef LLVM_TARGET_POWERPC_H
1616
#define LLVM_TARGET_POWERPC_H
1717

18+
#include <string>
19+
1820
// GCC #defines PPC on Linux but we use it as our namespace name
1921
#undef PPC
2022

@@ -30,13 +32,15 @@ namespace llvm {
3032
class MCCodeEmitter;
3133
class MCContext;
3234
class TargetMachine;
35+
class TargetAsmBackend;
3336

3437
FunctionPass *createPPCBranchSelectionPass();
3538
FunctionPass *createPPCISelDag(PPCTargetMachine &TM);
3639
FunctionPass *createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
3740
JITCodeEmitter &MCE);
3841
MCCodeEmitter *createPPCMCCodeEmitter(const Target &, TargetMachine &TM,
3942
MCContext &Ctx);
43+
TargetAsmBackend *createPPCAsmBackend(const Target &, const std::string &);
4044

4145
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
4246
AsmPrinter &AP);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===-- 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"
16+
#include "llvm/Target/TargetRegistry.h"
17+
#include "llvm/Support/MachO.h"
18+
using namespace llvm;
19+
20+
namespace {
21+
class PPCAsmBackend : public TargetAsmBackend {
22+
public:
23+
PPCAsmBackend(const Target &T) : TargetAsmBackend(T) {}
24+
25+
bool MayNeedRelaxation(const MCInst &Inst) const {
26+
// FIXME.
27+
return false;
28+
}
29+
30+
void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
31+
// FIXME.
32+
assert(0 && "RelaxInstruction() unimplemented");
33+
}
34+
35+
bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
36+
// FIXME: Zero fill for now. That's not right, but at least will get the
37+
// section size right.
38+
for (uint64_t i = 0; i != Count; ++i)
39+
OW->Write8(0);
40+
return true;
41+
}
42+
43+
unsigned getPointerSize() const {
44+
StringRef Name = TheTarget.getName();
45+
if (Name == "ppc64") return 8;
46+
assert(Name == "ppc32" && "Unknown target name!");
47+
return 4;
48+
}
49+
};
50+
} // end anonymous namespace
51+
52+
53+
// FIXME: This should be in a separate file.
54+
namespace {
55+
class DarwinPPCAsmBackend : public PPCAsmBackend {
56+
MCMachOObjectFormat Format;
57+
public:
58+
DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) {
59+
HasScatteredSymbols = true;
60+
}
61+
62+
virtual const MCObjectFormat &getObjectFormat() const {
63+
return Format;
64+
}
65+
66+
void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
67+
uint64_t Value) const {
68+
assert(0 && "UNIMP");
69+
}
70+
71+
bool isVirtualSection(const MCSection &Section) const {
72+
const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
73+
return (SMO.getType() == MCSectionMachO::S_ZEROFILL ||
74+
SMO.getType() == MCSectionMachO::S_GB_ZEROFILL ||
75+
SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL);
76+
}
77+
78+
MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
79+
bool is64 = getPointerSize() == 8;
80+
return createMachObjectWriter(OS, /*Is64Bit=*/is64,
81+
is64 ? MachO::CPUTypePowerPC64 :
82+
MachO::CPUTypePowerPC64,
83+
MachO::CPUSubType_POWERPC_ALL,
84+
/*IsLittleEndian=*/false);
85+
}
86+
87+
virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
88+
return false;
89+
}
90+
};
91+
} // end anonymous namespace
92+
93+
94+
95+
96+
TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
97+
const std::string &TT) {
98+
switch (Triple(TT).getOS()) {
99+
case Triple::Darwin:
100+
return new DarwinPPCAsmBackend(T);
101+
default:
102+
return 0;
103+
}
104+
}

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
438438
if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
439439
Directive = PPC::DIR_64;
440440
assert(Directive <= PPC::DIR_64 && "Directive out of range.");
441-
OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
441+
442+
// FIXME: This is a total hack, finish mc'izing the PPC backend.
443+
if (OutStreamer.hasRawTextSupport())
444+
OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
442445

443446
// Prime text sections so they are adjacent. This reduces the likelihood a
444447
// large data or debug section causes a branch to exceed 16M limit.

llvm/lib/Target/PowerPC/PPCTargetMachine.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "PPCMCAsmInfo.h"
1616
#include "PPCTargetMachine.h"
1717
#include "llvm/PassManager.h"
18+
#include "llvm/MC/MCStreamer.h"
1819
#include "llvm/Target/TargetOptions.h"
1920
#include "llvm/Target/TargetRegistry.h"
2021
#include "llvm/Support/FormattedStream.h"
@@ -29,6 +30,20 @@ static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
2930

3031
}
3132

33+
// This is duplicated code. Refactor this.
34+
static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
35+
MCContext &Ctx, TargetAsmBackend &TAB,
36+
raw_ostream &OS,
37+
MCCodeEmitter *Emitter,
38+
bool RelaxAll) {
39+
switch (Triple(TT).getOS()) {
40+
case Triple::Darwin:
41+
return createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
42+
default:
43+
return NULL;
44+
}
45+
}
46+
3247
extern "C" void LLVMInitializePowerPCTarget() {
3348
// Register the targets
3449
RegisterTargetMachine<PPC32TargetMachine> A(ThePPC32Target);
@@ -40,6 +55,15 @@ extern "C" void LLVMInitializePowerPCTarget() {
4055
// Register the MC Code Emitter
4156
TargetRegistry::RegisterCodeEmitter(ThePPC32Target, createPPCMCCodeEmitter);
4257
TargetRegistry::RegisterCodeEmitter(ThePPC64Target, createPPCMCCodeEmitter);
58+
59+
60+
// Register the asm backend.
61+
TargetRegistry::RegisterAsmBackend(ThePPC32Target, createPPCAsmBackend);
62+
TargetRegistry::RegisterAsmBackend(ThePPC64Target, createPPCAsmBackend);
63+
64+
// Register the object streamer.
65+
TargetRegistry::RegisterObjectStreamer(ThePPC32Target, createMCStreamer);
66+
TargetRegistry::RegisterObjectStreamer(ThePPC64Target, createMCStreamer);
4367
}
4468

4569

0 commit comments

Comments
 (0)