blob: c309ef7597171ac0c86a98c697df01ea8e9129a6 [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
13#include "ELFWriter.h"
14#include "llvm/CodeGen/MachineCodeEmitter.h"
15#include <vector>
16
17namespace llvm {
18
19 /// ELFCodeEmitter - This class is used by the ELFWriter to
20 /// emit the code for functions to the ELF file.
21 class ELFCodeEmitter : public MachineCodeEmitter {
22 ELFWriter &EW;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000023
24 /// Target machine description
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000025 TargetMachine &TM;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000026
27 /// Section containing code for functions
28 ELFSection *ES;
29
30 /// Relocations - These are the relocations that the function needs, as
31 /// emitted.
32 std::vector<MachineRelocation> Relocations;
33
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000034 /// CPLocations - This is a map of constant pool indices to offsets from the
35 /// start of the section for that constant pool index.
36 std::vector<uintptr_t> CPLocations;
37
38 /// CPSections - This is a map of constant pool indices to the MachOSection
39 /// containing the constant pool entry for that index.
40 std::vector<unsigned> CPSections;
41
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000042 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
43 /// It is filled in by the StartMachineBasicBlock callback and queried by
44 /// the getMachineBasicBlockAddress callback.
45 std::vector<uintptr_t> MBBLocations;
46
47 /// FnStartPtr - Pointer to the start location of the current function
48 /// in the buffer
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000049 uint8_t *FnStartPtr;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000050 public:
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000051 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM) {}
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000052
53 void startFunction(MachineFunction &F);
54 bool finishFunction(MachineFunction &F);
55
56 void addRelocation(const MachineRelocation &MR) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000057 Relocations.push_back(MR);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000058 }
59
60 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000061 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
62 MBBLocations.resize((MBB->getNumber()+1)*2);
63 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
64 }
65
66 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) {
67 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
68 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
69 return MBBLocations[MBB->getNumber()];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000070 }
71
72 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000073 assert(CPLocations.size() > Index && "CP not emitted!");
74 return CPLocations[Index];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000075 }
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000076
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000077 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
78 assert(0 && "JT not implementated yet!");
79 return 0;
80 }
81
82 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
83 assert(0 && "JT not implementated yet!");
84 return 0;
85 }
86
87 virtual uintptr_t getLabelAddress(uint64_t Label) const {
88 assert(0 && "Label address not implementated yet!");
89 abort();
90 return 0;
91 }
92
93 virtual void emitLabel(uint64_t LabelID) {
94 assert(0 && "emit Label not implementated yet!");
95 abort();
96 }
97
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000098 /// emitConstantPool - For each constant pool entry, figure out which section
99 /// the constant should live in and emit the constant.
100 void emitConstantPool(MachineConstantPool *MCP);
101
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000102 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
103
104 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
105 void startGVStub(const GlobalValue* F, unsigned StubSize,
106 unsigned Alignment = 1) {
107 assert(0 && "JIT specific function called!");
108 abort();
109 }
110 void startGVStub(const GlobalValue* F, void *Buffer, unsigned StubSize) {
111 assert(0 && "JIT specific function called!");
112 abort();
113 }
114 void *finishGVStub(const GlobalValue *F) {
115 assert(0 && "JIT specific function called!");
116 abort();
117 return 0;
118 }
119}; // end class ELFCodeEmitter
120
121} // end namespace llvm
122
123#endif
124