blob: 982aebf8fcc022ce1ec172c50591be8026539fd0 [file] [log] [blame]
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +00001//===-- lib/CodeGen/ELFCodeEmitter.h ----------------------------*- C++ -*-===//
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#ifndef ELFCODEEMITTER_H
11#define ELFCODEEMITTER_H
12
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000013#include "llvm/CodeGen/MachineCodeEmitter.h"
14#include <vector>
15
16namespace llvm {
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000017 class ELFWriter;
18 class ELFSection;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000019
20 /// ELFCodeEmitter - This class is used by the ELFWriter to
21 /// emit the code for functions to the ELF file.
22 class ELFCodeEmitter : public MachineCodeEmitter {
23 ELFWriter &EW;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000024
25 /// Target machine description
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000026 TargetMachine &TM;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000027
28 /// Section containing code for functions
29 ELFSection *ES;
30
31 /// Relocations - These are the relocations that the function needs, as
32 /// emitted.
33 std::vector<MachineRelocation> Relocations;
34
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000035 /// CPLocations - This is a map of constant pool indices to offsets from the
36 /// start of the section for that constant pool index.
37 std::vector<uintptr_t> CPLocations;
38
39 /// CPSections - This is a map of constant pool indices to the MachOSection
40 /// containing the constant pool entry for that index.
41 std::vector<unsigned> CPSections;
42
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000043 /// JTLocations - This is a map of jump table indices to offsets from the
44 /// start of the section for that jump table index.
45 std::vector<uintptr_t> JTLocations;
46
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000047 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
48 /// It is filled in by the StartMachineBasicBlock callback and queried by
49 /// the getMachineBasicBlockAddress callback.
50 std::vector<uintptr_t> MBBLocations;
51
52 /// FnStartPtr - Pointer to the start location of the current function
53 /// in the buffer
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000054 uint8_t *FnStartPtr;
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000055
56 /// JumpTableSectionIdx - Holds the index of the Jump Table Section
57 unsigned JumpTableSectionIdx;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000058 public:
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000059 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM),
60 JumpTableSectionIdx(0) {}
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000061
62 void startFunction(MachineFunction &F);
63 bool finishFunction(MachineFunction &F);
64
65 void addRelocation(const MachineRelocation &MR) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000066 Relocations.push_back(MR);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000067 }
68
69 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000070 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
71 MBBLocations.resize((MBB->getNumber()+1)*2);
72 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
73 }
74
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000075 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000076 assert(CPLocations.size() > Index && "CP not emitted!");
77 return CPLocations[Index];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000078 }
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000079
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000080 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000081 assert(JTLocations.size() > Index && "JT not emitted!");
82 return JTLocations[Index];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000083 }
84
85 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +000086 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
87 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
88 return MBBLocations[MBB->getNumber()];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000089 }
90
91 virtual uintptr_t getLabelAddress(uint64_t Label) const {
92 assert(0 && "Label address not implementated yet!");
93 abort();
94 return 0;
95 }
96
97 virtual void emitLabel(uint64_t LabelID) {
98 assert(0 && "emit Label not implementated yet!");
99 abort();
100 }
101
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000102 /// emitConstantPool - For each constant pool entry, figure out which section
103 /// the constant should live in and emit the constant.
104 void emitConstantPool(MachineConstantPool *MCP);
105
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000106 /// emitJumpTables - Emit all the jump tables for a given jump table info
107 /// record to the appropriate section.
108 void emitJumpTables(MachineJumpTableInfo *MJTI);
109
110 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) {}
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000111
112 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
113 void startGVStub(const GlobalValue* F, unsigned StubSize,
114 unsigned Alignment = 1) {
115 assert(0 && "JIT specific function called!");
116 abort();
117 }
118 void startGVStub(const GlobalValue* F, void *Buffer, unsigned StubSize) {
119 assert(0 && "JIT specific function called!");
120 abort();
121 }
122 void *finishGVStub(const GlobalValue *F) {
123 assert(0 && "JIT specific function called!");
124 abort();
125 return 0;
126 }
127}; // end class ELFCodeEmitter
128
129} // end namespace llvm
130
131#endif
132