Make LABEL a builtin opcode.
[oota-llvm.git] / lib / Target / PowerPC / PPCMachOWriter.cpp
1 //===-- PPCMachOWriter.cpp - Emit a Mach-O file for the PowerPC backend ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Nate Begeman and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a Mach-O writer for the PowerPC backend.  The public
11 // interface to this file is the createPPCMachOObjectWriterPass function.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPCRelocations.h"
16 #include "PPCTargetMachine.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/MachOWriter.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/OutputBuffer.h"
21 using namespace llvm;
22
23 namespace {
24   class VISIBILITY_HIDDEN PPCMachOWriter : public MachOWriter {
25   public:
26     PPCMachOWriter(std::ostream &O, PPCTargetMachine &TM)
27       : MachOWriter(O, TM) {}
28
29     virtual void GetTargetRelocation(MachineRelocation &MR, MachOSection &From,
30                                      MachOSection &To);
31
32     // Constants for the relocation r_type field.
33     // see <mach-o/ppc/reloc.h>
34     enum { PPC_RELOC_VANILLA, // generic relocation
35            PPC_RELOC_PAIR,    // the second relocation entry of a pair
36            PPC_RELOC_BR14,    // 14 bit branch displacement to word address
37            PPC_RELOC_BR24,    // 24 bit branch displacement to word address
38            PPC_RELOC_HI16,    // a PAIR follows with the low 16 bits
39            PPC_RELOC_LO16,    // a PAIR follows with the high 16 bits
40            PPC_RELOC_HA16,    // a PAIR follows, which is sign extended to 32b
41            PPC_RELOC_LO14     // LO16 with low 2 bits implicitly zero
42     };
43   };
44 }
45
46 /// addPPCMachOObjectWriterPass - Returns a pass that outputs the generated code
47 /// as a Mach-O object file.
48 ///
49 void llvm::addPPCMachOObjectWriterPass(FunctionPassManager &FPM,
50                                        std::ostream &O, PPCTargetMachine &TM) {
51   PPCMachOWriter *MOW = new PPCMachOWriter(O, TM);
52   FPM.add(MOW);
53   FPM.add(createPPCCodeEmitterPass(TM, MOW->getMachineCodeEmitter()));
54 }
55
56 /// GetTargetRelocation - For the MachineRelocation MR, convert it to one or
57 /// more PowerPC MachORelocation(s), add the new relocations to the
58 /// MachOSection, and rewrite the instruction at the section offset if required
59 /// by that relocation type.
60 void PPCMachOWriter::GetTargetRelocation(MachineRelocation &MR,
61                                          MachOSection &From,
62                                          MachOSection &To) {
63   uint64_t Addr = 0;
64
65   // Keep track of whether or not this is an externally defined relocation.
66   bool     isExtern = false;
67
68   // Get the address of whatever it is we're relocating, if possible.
69   if (!isExtern)
70     Addr = (uintptr_t)MR.getResultPointer() + To.addr;
71
72   switch ((PPC::RelocationType)MR.getRelocationType()) {
73   default: assert(0 && "Unknown PPC relocation type!");
74   case PPC::reloc_absolute_low_ix:
75     assert(0 && "Unhandled PPC relocation type!");
76     break;
77   case PPC::reloc_vanilla:
78     {
79       // FIXME: need to handle 64 bit vanilla relocs
80       MachORelocation VANILLA(MR.getMachineCodeOffset(), To.Index, false, 2,
81                               isExtern, PPC_RELOC_VANILLA);
82       ++From.nreloc;
83
84       OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
85       RelocOut.outword(VANILLA.r_address);
86       RelocOut.outword(VANILLA.getPackedFields());
87
88       OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
89       SecOut.fixword(Addr, MR.getMachineCodeOffset());
90       break;
91     }
92   case PPC::reloc_pcrel_bx:
93     {
94       Addr -= MR.getMachineCodeOffset();
95       Addr >>= 2;
96       Addr &= 0xFFFFFF;
97       Addr <<= 2;
98       Addr |= (From.SectionData[MR.getMachineCodeOffset()] << 24);
99
100       OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
101       SecOut.fixword(Addr, MR.getMachineCodeOffset());
102       break;
103     }
104   case PPC::reloc_pcrel_bcx:
105     {
106       Addr -= MR.getMachineCodeOffset();
107       Addr &= 0xFFFC;
108
109       OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
110       SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
111       break;
112     }
113   case PPC::reloc_absolute_high:
114     {
115       MachORelocation HA16(MR.getMachineCodeOffset(), To.Index, false, 2,
116                            isExtern, PPC_RELOC_HA16);
117       MachORelocation PAIR(Addr & 0xFFFF, 0xFFFFFF, false, 2, isExtern,
118                            PPC_RELOC_PAIR);
119       ++From.nreloc;
120       ++From.nreloc;
121
122       OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
123       RelocOut.outword(HA16.r_address);
124       RelocOut.outword(HA16.getPackedFields());
125       RelocOut.outword(PAIR.r_address);
126       RelocOut.outword(PAIR.getPackedFields());
127       printf("ha16: %x\n", (unsigned)Addr);
128       Addr += 0x8000;
129
130       OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
131       SecOut.fixhalf(Addr >> 16, MR.getMachineCodeOffset() + 2);
132       break;
133     }
134   case PPC::reloc_absolute_low:
135     {
136       MachORelocation LO16(MR.getMachineCodeOffset(), To.Index, false, 2,
137                            isExtern, PPC_RELOC_LO16);
138       MachORelocation PAIR(Addr >> 16, 0xFFFFFF, false, 2, isExtern,
139                            PPC_RELOC_PAIR);
140       ++From.nreloc;
141       ++From.nreloc;
142
143       OutputBuffer RelocOut(From.RelocBuffer, is64Bit, isLittleEndian);
144       RelocOut.outword(LO16.r_address);
145       RelocOut.outword(LO16.getPackedFields());
146       RelocOut.outword(PAIR.r_address);
147       RelocOut.outword(PAIR.getPackedFields());
148       printf("lo16: %x\n", (unsigned)Addr);
149
150       OutputBuffer SecOut(From.SectionData, is64Bit, isLittleEndian);
151       SecOut.fixhalf(Addr, MR.getMachineCodeOffset() + 2);
152       break;
153     }
154   }
155 }