blob: 476ca45a229c0b8c04c147876593b6bbc8233f14 [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 Lattnerbd199fb2002-12-24 00:01:05 +000015#include "Support/Statistic.h"
Chris Lattner08d5e1f2003-06-08 06:43:57 +000016#include <stdio.h>
Chris Lattnerbd199fb2002-12-24 00:01:05 +000017
18namespace {
19 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
Misha Brukmand69c1e62003-07-28 19:09:06 +000020 VM *TheVM = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021
22 class Emitter : public MachineCodeEmitter {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000023 // CurBlock - The start of the current block of memory. CurByte - The
24 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +000025 unsigned char *CurBlock, *CurByte;
26
27 // When outputting a function stub in the context of some other function, we
28 // save CurBlock and CurByte here.
29 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000030
31 // ConstantPoolAddresses - Contains the location for each entry in the
32 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +000033 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034 public:
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000035 Emitter(VM &vm) { TheVM = &vm; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000036
37 virtual void startFunction(MachineFunction &F);
38 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +000039 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +000040 virtual void startFunctionStub(const Function &F, unsigned StubSize);
41 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000042 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000043 virtual void emitWord(unsigned W);
44
45 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
46 virtual uint64_t getGlobalValueAddress(const std::string &Name);
47 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
48 virtual uint64_t getCurrentPCValue();
49
50 // forceCompilationOf - Force the compilation of the specified function, and
51 // return its address, because we REALLY need the address now.
52 //
53 // FIXME: This is JIT specific!
54 //
55 virtual uint64_t forceCompilationOf(Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000056 };
57}
58
Misha Brukman906f5fa2003-06-02 03:23:16 +000059MachineCodeEmitter *VM::createEmitter(VM &V) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000060 return new Emitter(V);
61}
62
63
64#define _POSIX_MAPPED_FILES
65#include <unistd.h>
66#include <sys/mman.h>
67
Chris Lattner6125fdd2003-05-09 03:30:07 +000068// FIXME: This should be rewritten to support a real memory manager for
69// executable memory pages!
70static void *getMemory(unsigned NumPages) {
Misha Brukman906f5fa2003-06-02 03:23:16 +000071 void *pa;
72 if (NumPages == 0) return 0;
Misha Brukman3a55e8a2003-06-04 19:45:25 +000073 static const long pageSize = sysconf(_SC_PAGESIZE);
74
75#if defined(i386) || defined(__i386__) || defined(__x86__)
Misha Brukmanfd31a782003-07-28 19:26:19 +000076 /* Linux and *BSD tend to have these flags named differently. */
Brian Gaeke4399a492003-06-17 23:14:06 +000077#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
78# define MAP_ANONYMOUS MAP_ANON
Misha Brukman89e8be02003-07-29 16:57:16 +000079#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
80#define fd 0
Misha Brukman3a55e8a2003-06-04 19:45:25 +000081#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman89e8be02003-07-29 16:57:16 +000082#define fd -1
Misha Brukman3a55e8a2003-06-04 19:45:25 +000083#else
Misha Brukman89e8be02003-07-29 16:57:16 +000084 std::cerr << "This architecture is not supported by the JIT!\n";
Misha Brukman3a55e8a2003-06-04 19:45:25 +000085 abort();
86#endif
Misha Brukman89e8be02003-07-29 16:57:16 +000087 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
88 MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
Misha Brukman906f5fa2003-06-02 03:23:16 +000089 if (pa == MAP_FAILED) {
90 perror("mmap");
91 abort();
92 }
93 return pa;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000094}
95
96
97void Emitter::startFunction(MachineFunction &F) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000098 CurBlock = (unsigned char *)getMemory(16);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000099 CurByte = CurBlock; // Start writing at the beginning of the fn.
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000100 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000101}
102
103void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000104 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000105 NumBytes += CurByte-CurBlock;
106
Brian Gaeke02c26b62003-06-30 18:06:20 +0000107 DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000108 << "] Function: " << F.getFunction()->getName()
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000109 << ": " << CurByte-CurBlock << " bytes of text\n");
110}
111
Chris Lattner1cc08382003-01-13 01:00:12 +0000112void Emitter::emitConstantPool(MachineConstantPool *MCP) {
113 const std::vector<Constant*> &Constants = MCP->getConstants();
114 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
115 // For now we just allocate some memory on the heap, this can be
116 // dramatically improved.
117 const Type *Ty = ((Value*)Constants[i])->getType();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000118 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
119 TheVM->InitializeMemory(Constants[i], Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000120 ConstantPoolAddresses.push_back(Addr);
121 }
122}
123
Chris Lattner6125fdd2003-05-09 03:30:07 +0000124void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000125 static const long pageSize = sysconf(_SC_PAGESIZE);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000126 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
127 // FIXME: this is a huge waste of memory.
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000128 CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000129 CurByte = CurBlock; // Start writing at the beginning of the fn.
130}
131
132void *Emitter::finishFunctionStub(const Function &F) {
133 NumBytes += CurByte-CurBlock;
134 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
135 << (unsigned)(intptr_t)CurBlock
136 << std::dec << "] Function stub for: " << F.getName()
137 << ": " << CurByte-CurBlock << " bytes of text\n");
138 std::swap(CurBlock, SavedCurBlock);
139 CurByte = SavedCurByte;
140 return SavedCurBlock;
141}
142
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000143void Emitter::emitByte(unsigned char B) {
144 *CurByte++ = B; // Write the byte to memory
145}
146
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000147void Emitter::emitWord(unsigned W) {
148 // FIXME: This won't work if the endianness of the host and target don't
149 // agree! (For a JIT this can't happen though. :)
150 *(unsigned*)CurByte = W;
151 CurByte += sizeof(unsigned);
152}
153
154
155uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
156 // Try looking up the function to see if it is already compiled, if not return
157 // 0.
158 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
159}
160uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
161 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
162}
163
164// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
165// in the constant pool that was last emitted with the 'emitConstantPool'
166// method.
167//
168uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
169 assert(ConstantNum < ConstantPoolAddresses.size() &&
170 "Invalid ConstantPoolIndex!");
171 return (intptr_t)ConstantPoolAddresses[ConstantNum];
172}
173
174// getCurrentPCValue - This returns the address that the next emitted byte
175// will be output to.
176//
177uint64_t Emitter::getCurrentPCValue() {
178 return (intptr_t)CurByte;
179}
180
181uint64_t Emitter::forceCompilationOf(Function *F) {
182 return (intptr_t)TheVM->getPointerToFunction(F);
183}
184
Misha Brukmand69c1e62003-07-28 19:09:06 +0000185// getPointerToNamedFunction - This function is used as a global wrapper to
186// VM::getPointerToNamedFunction for the purpose of resolving symbols when
187// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
188// need to resolve function(s) that are being mis-codegenerated, so we need to
189// resolve their addresses at runtime, and this is the way to do it.
190extern "C" {
191 void *getPointerToNamedFunction(const char *Name) {
192 Module &M = TheVM->getModule();
193 if (Function *F = M.getNamedFunction(Name))
194 return TheVM->getPointerToFunction(F);
195 return TheVM->getPointerToNamedFunction(Name);
196 }
197}