Wire up primitive support in the assembler backend for writing .o files
[oota-llvm.git] / lib / Target / PowerPC / PPCAsmBackend.cpp
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 }