blob: ee98778157992f171e66f8f6fb99b16b3a9cd22a [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- Emitter.cpp - Write machine code to executable memory -------------===//
2//
3// This file defines a MachineCodeEmitter object that is used by Jello to write
4// machine code to memory and remember where relocatable values lie.
5//
6//===----------------------------------------------------------------------===//
7
8#include "VM.h"
John Criswell7a73b802003-06-30 21:59:07 +00009#include "Config/sys/mman.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010#include "llvm/CodeGen/MachineCodeEmitter.h"
11#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000012#include "llvm/CodeGen/MachineConstantPool.h"
13#include "llvm/Target/TargetData.h"
Misha Brukmand69c1e62003-07-28 19:09:06 +000014#include "llvm/Module.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000015#include "Support/Debug.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000016#include "Support/Statistic.h"
Chris Lattner08d5e1f2003-06-08 06:43:57 +000017#include <stdio.h>
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018
19namespace {
20 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
Misha Brukmand69c1e62003-07-28 19:09:06 +000021 VM *TheVM = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000022
23 class Emitter : public MachineCodeEmitter {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000024 // CurBlock - The start of the current block of memory. CurByte - The
25 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +000026 unsigned char *CurBlock, *CurByte;
27
28 // When outputting a function stub in the context of some other function, we
29 // save CurBlock and CurByte here.
30 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000031
32 // ConstantPoolAddresses - Contains the location for each entry in the
33 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +000034 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035 public:
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000036 Emitter(VM &vm) { TheVM = &vm; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000037
38 virtual void startFunction(MachineFunction &F);
39 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +000040 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +000041 virtual void startFunctionStub(const Function &F, unsigned StubSize);
42 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000043 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000044 virtual void emitWord(unsigned W);
45
46 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
47 virtual uint64_t getGlobalValueAddress(const std::string &Name);
48 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
49 virtual uint64_t getCurrentPCValue();
50
51 // forceCompilationOf - Force the compilation of the specified function, and
52 // return its address, because we REALLY need the address now.
53 //
54 // FIXME: This is JIT specific!
55 //
56 virtual uint64_t forceCompilationOf(Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000057 };
58}
59
Misha Brukman906f5fa2003-06-02 03:23:16 +000060MachineCodeEmitter *VM::createEmitter(VM &V) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000061 return new Emitter(V);
62}
63
64
65#define _POSIX_MAPPED_FILES
66#include <unistd.h>
67#include <sys/mman.h>
68
Chris Lattner6125fdd2003-05-09 03:30:07 +000069// FIXME: This should be rewritten to support a real memory manager for
70// executable memory pages!
71static void *getMemory(unsigned NumPages) {
Misha Brukman906f5fa2003-06-02 03:23:16 +000072 void *pa;
73 if (NumPages == 0) return 0;
Misha Brukman3a55e8a2003-06-04 19:45:25 +000074 static const long pageSize = sysconf(_SC_PAGESIZE);
75
76#if defined(i386) || defined(__i386__) || defined(__x86__)
Misha Brukmanfd31a782003-07-28 19:26:19 +000077 /* Linux and *BSD tend to have these flags named differently. */
Brian Gaeke4399a492003-06-17 23:14:06 +000078#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
79# define MAP_ANONYMOUS MAP_ANON
Misha Brukman89e8be02003-07-29 16:57:16 +000080#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
81#define fd 0
Misha Brukman3a55e8a2003-06-04 19:45:25 +000082#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman89e8be02003-07-29 16:57:16 +000083#define fd -1
Misha Brukman3a55e8a2003-06-04 19:45:25 +000084#else
Misha Brukman89e8be02003-07-29 16:57:16 +000085 std::cerr << "This architecture is not supported by the JIT!\n";
Misha Brukman3a55e8a2003-06-04 19:45:25 +000086 abort();
87#endif
Misha Brukman89e8be02003-07-29 16:57:16 +000088 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
89 MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
Misha Brukman906f5fa2003-06-02 03:23:16 +000090 if (pa == MAP_FAILED) {
91 perror("mmap");
92 abort();
93 }
94 return pa;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000095}
96
97
98void Emitter::startFunction(MachineFunction &F) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000099 CurBlock = (unsigned char *)getMemory(16);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000100 CurByte = CurBlock; // Start writing at the beginning of the fn.
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000101 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000102}
103
104void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000105 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000106 NumBytes += CurByte-CurBlock;
107
Brian Gaeke02c26b62003-06-30 18:06:20 +0000108 DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000109 << "] Function: " << F.getFunction()->getName()
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000110 << ": " << CurByte-CurBlock << " bytes of text\n");
111}
112
Chris Lattner1cc08382003-01-13 01:00:12 +0000113void Emitter::emitConstantPool(MachineConstantPool *MCP) {
114 const std::vector<Constant*> &Constants = MCP->getConstants();
115 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
116 // For now we just allocate some memory on the heap, this can be
117 // dramatically improved.
118 const Type *Ty = ((Value*)Constants[i])->getType();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000119 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
120 TheVM->InitializeMemory(Constants[i], Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000121 ConstantPoolAddresses.push_back(Addr);
122 }
123}
124
Chris Lattner6125fdd2003-05-09 03:30:07 +0000125void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000126 static const long pageSize = sysconf(_SC_PAGESIZE);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000127 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
128 // FIXME: this is a huge waste of memory.
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000129 CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000130 CurByte = CurBlock; // Start writing at the beginning of the fn.
131}
132
133void *Emitter::finishFunctionStub(const Function &F) {
134 NumBytes += CurByte-CurBlock;
135 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
136 << (unsigned)(intptr_t)CurBlock
137 << std::dec << "] Function stub for: " << F.getName()
138 << ": " << CurByte-CurBlock << " bytes of text\n");
139 std::swap(CurBlock, SavedCurBlock);
140 CurByte = SavedCurByte;
141 return SavedCurBlock;
142}
143
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000144void Emitter::emitByte(unsigned char B) {
145 *CurByte++ = B; // Write the byte to memory
146}
147
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000148void Emitter::emitWord(unsigned W) {
149 // FIXME: This won't work if the endianness of the host and target don't
150 // agree! (For a JIT this can't happen though. :)
151 *(unsigned*)CurByte = W;
152 CurByte += sizeof(unsigned);
153}
154
155
156uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
157 // Try looking up the function to see if it is already compiled, if not return
158 // 0.
159 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
160}
161uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
162 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
163}
164
165// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
166// in the constant pool that was last emitted with the 'emitConstantPool'
167// method.
168//
169uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
170 assert(ConstantNum < ConstantPoolAddresses.size() &&
171 "Invalid ConstantPoolIndex!");
172 return (intptr_t)ConstantPoolAddresses[ConstantNum];
173}
174
175// getCurrentPCValue - This returns the address that the next emitted byte
176// will be output to.
177//
178uint64_t Emitter::getCurrentPCValue() {
179 return (intptr_t)CurByte;
180}
181
182uint64_t Emitter::forceCompilationOf(Function *F) {
183 return (intptr_t)TheVM->getPointerToFunction(F);
184}
185
Misha Brukmand69c1e62003-07-28 19:09:06 +0000186// getPointerToNamedFunction - This function is used as a global wrapper to
187// VM::getPointerToNamedFunction for the purpose of resolving symbols when
188// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
189// need to resolve function(s) that are being mis-codegenerated, so we need to
190// resolve their addresses at runtime, and this is the way to do it.
191extern "C" {
192 void *getPointerToNamedFunction(const char *Name) {
193 Module &M = TheVM->getModule();
194 if (Function *F = M.getNamedFunction(Name))
195 return TheVM->getPointerToFunction(F);
196 return TheVM->getPointerToNamedFunction(Name);
197 }
198}