-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbfjit.cpp
116 lines (99 loc) · 3.87 KB
/
bfjit.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// bfjit.cpp
// brainfuck
//
// Created by Xu Chen on 12-9-8.
// Copyright (c) 2012 Xu Chen. All rights reserved.
//
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#ifndef __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
#endif
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS
#endif
#include <stdio.h>
#include <llvm/LLVMContext.h>
#include <llvm/DerivedTypes.h>
#include <llvm/Module.h>
#include <llvm/Value.h>
#include <llvm/PassManager.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Target/TargetData.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/JIT.h>
#include "bfast.h"
#include "bfparser.h"
#include "bfcodegen.h"
#include "bfjit.h"
using namespace llvm;
namespace brainfuck {
namespace jit {
struct jit_engine {
jit_engine(int optimization_level=0);
main_func_type compile(const ast::Program &prog);
int optimization_level_;
}; // End of jit_engine
jit_engine::jit_engine(int optimization_level)
: optimization_level_(optimization_level)
{
llvm::InitializeNativeTarget();
}
main_func_type jit_engine::compile(const ast::Program &prog) {
llvm::LLVMContext &c=llvm::getGlobalContext();
llvm::Module *m=new llvm::Module("brainfuck", c);
brainfuck::codegen(*m, prog);
//m->dump();
std::string ErrStr;
ExecutionEngine *TheExecutionEngine = EngineBuilder(m).setErrorStr(&ErrStr).create();
if (!TheExecutionEngine) {
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
exit(1);
}
Function *f_main=m->getFunction("main");
// Optimize, target dependant
if(optimization_level_>0)
{
FunctionPassManager OurFPM(m);
// Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
// Provide basic AliasAnalysis support for GVN.
OurFPM.add(createBasicAliasAnalysisPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions.
OurFPM.add(createReassociatePass());
// Eliminate Common SubExpressions.
OurFPM.add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass());
//OurFPM.add(createLoopSimplifyPass());
//OurFPM.add(createBlockPlacementPass());
//OurFPM.add(createConstantPropagationPass());
OurFPM.doInitialization();
OurFPM.run(*f_main);
}
//m->dump();
void *rp=TheExecutionEngine->getPointerToFunction(f_main);
main_func_type fp=reinterpret_cast<main_func_type>(rp);
return fp;
}
main_func_type compile(std::istream &src) {
jit_engine engine;
ast::Program prog;
std::string s((std::istreambuf_iterator<char>(src)),
std::istreambuf_iterator<char>());
bool ret=parser::parse(s.begin(), s.end(), prog);
if (!ret) {
std::cerr << "Syntax error\n";
exit(1);
}
return engine.compile(prog);
}
} // End of namespace jit
} // End of namespace brainfuck