forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLVMExtra.cpp
72 lines (65 loc) · 2.08 KB
/
LLVMExtra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#include "LLVMExtra.h"
#include "llvm-c/Disassembler.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/PassManager.h"
#include <iostream>
using namespace llvm;
LLVMMemoryBufferRef
LLVMExtraCreateMemoryBufferWithPtr(const char *ptr, size_t length)
{
return wrap(MemoryBuffer::getMemBuffer(StringRef(ptr, length)));
}
LLVMBool LLVMExtraInlineFunction(LLVMValueRef call)
{
InlineFunctionInfo IFI;
return InlineFunction(CallSite(unwrap(call)), IFI);
}
void LLVMExtraAddDeadCodeEliminationPass(LLVMPassManagerRef PM) {
unwrap(PM)->add(createDeadCodeEliminationPass());
}
#if 0
class JitDisassembler : public JITEventListener {
LLVMDisasmContextRef DC;
public:
JitDisassembler(const char *triple) {
DC = LLVMCreateDisasm(triple, 0, 0, 0, 0);
}
~JitDisassembler() {
LLVMDisasmDispose(DC);
}
virtual void NotifyFunctionEmitted(const Function &f, void *code,
size_t size,
const EmittedFunctionDetails &) {
uint8_t *bytes = (uint8_t*)code;
uint8_t *end = bytes + size;
char buf[256];
while (bytes < end) {
size_t instSize = LLVMDisasmInstruction(DC, bytes, end - bytes,
(uint64_t)bytes, &buf[0], 256);
if (instSize == 0) {
std::cerr << "\t???\n";
return;
}
bytes += instSize;
std::cerr << buf << '\n';
}
}
};
#endif
void
LLVMExtraRegisterJitDisassembler(LLVMExecutionEngineRef EE,
const char *triple) {
#if 0
static JitDisassembler disassembler(triple);
unwrap(EE)->RegisterJITEventListener(&disassembler);
#endif
}