blob: e3d3618474841e2152d6ae2572f115244891f6cf [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 Lopesa029a272009-06-07 21:22:38 +000043 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
44 /// It is filled in by the StartMachineBasicBlock callback and queried by
45 /// the getMachineBasicBlockAddress callback.
46 std::vector<uintptr_t> MBBLocations;
47
48 /// FnStartPtr - Pointer to the start location of the current function
49 /// in the buffer
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000050 uint8_t *FnStartPtr;
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000051 public:
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +000052 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM) {}
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000053
54 void startFunction(MachineFunction &F);
55 bool finishFunction(MachineFunction &F);
56
57 void addRelocation(const MachineRelocation &MR) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000058 Relocations.push_back(MR);
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000059 }
60
61 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000062 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
63 MBBLocations.resize((MBB->getNumber()+1)*2);
64 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
65 }
66
67 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) {
68 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
69 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
70 return MBBLocations[MBB->getNumber()];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000071 }
72
73 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000074 assert(CPLocations.size() > Index && "CP not emitted!");
75 return CPLocations[Index];
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000076 }
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000077
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000078 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
79 assert(0 && "JT not implementated yet!");
80 return 0;
81 }
82
83 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
84 assert(0 && "JT not implementated yet!");
85 return 0;
86 }
87
88 virtual uintptr_t getLabelAddress(uint64_t Label) const {
89 assert(0 && "Label address not implementated yet!");
90 abort();
91 return 0;
92 }
93
94 virtual void emitLabel(uint64_t LabelID) {
95 assert(0 && "emit Label not implementated yet!");
96 abort();
97 }
98
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +000099 /// emitConstantPool - For each constant pool entry, figure out which section
100 /// the constant should live in and emit the constant.
101 void emitConstantPool(MachineConstantPool *MCP);
102
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +0000103 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
104
105 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
106 void startGVStub(const GlobalValue* F, unsigned StubSize,
107 unsigned Alignment = 1) {
108 assert(0 && "JIT specific function called!");
109 abort();
110 }
111 void startGVStub(const GlobalValue* F, void *Buffer, unsigned StubSize) {
112 assert(0 && "JIT specific function called!");
113 abort();
114 }
115 void *finishGVStub(const GlobalValue *F) {
116 assert(0 && "JIT specific function called!");
117 abort();
118 return 0;
119 }
120}; // end class ELFCodeEmitter
121
122} // end namespace llvm
123
124#endif
125