-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlang_1-llvm.cxx
179 lines (150 loc) · 6.69 KB
/
lang_1-llvm.cxx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// (C) Bernard Hugueney, licence GPL v3
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Function.h>
#include <llvm/PassManager.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/Support/IRBuilder.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/DerivedTypes.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Target/TargetData.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/ExecutionEngine/JIT.h>
// see http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20101115/112020.html
#include <llvm/Support/NoFolder.h>
using namespace llvm;
// g++ -o lang_1-llvm lang_1-llvm.cxx `llvm-config-2.8 --cppflags --ldflags --libs ` -lstdc++ -rdynamic
using namespace boost::spirit;
using namespace boost::spirit::qi;
using namespace boost::spirit::ascii;
// template function to map C++ type -> llvm::Type
template<typename T> static const llvm::Type* type(){ return "unkown type!";}// error
#define MAP_INT_TYPE(cpp_type_) \
template <> \
const llvm::Type* type<cpp_type_>() { \
return llvm::Type::getIntNTy(llvm::getGlobalContext() \
, sizeof(cpp_type_)*8); \
}
#define MAP_TYPE(cpp_type_, fn_) \
template <> \
const llvm::Type* type<cpp_type_>() { \
return llvm::Type::fn_(llvm::getGlobalContext()); \
}
MAP_TYPE(void, getVoidTy)
MAP_INT_TYPE(char)
MAP_INT_TYPE(short)
MAP_INT_TYPE(int)
MAP_INT_TYPE(long)
MAP_INT_TYPE(long long)
MAP_TYPE(float, getFloatTy)
MAP_TYPE(double, getDoubleTy)
// long double is more tricky
#undef MAP_INT_TYPE
#undef MAP_TYPE
// arithmetic expression grammar, using actions to insert nodes into the AST
template <typename value_t, typename Iterator, typename Builder>
struct language_1_grammar : grammar<Iterator, Module*(), boost::spirit::standard::space_type> {
struct builder_helper{
template<typename T1, typename T2=T1, typename T3=T2, typename T4=T3> struct result{ typedef Value* type;};
builder_helper(Builder& b):builder(b){}
typedef Value* (Builder::*binary_op_t)(Value*, Value*, const Twine&);
typedef Value* (Builder::*unary_op_t)(Value*, const Twine&);
typedef ReturnInst* (Builder::*op_no_name )(Value*);
Value* operator()(binary_op_t op, Value* a1, Value* a2, const char * name) const
{ return (builder.*op)(a1, a2, name); }
Value* operator()(unary_op_t op, Value* a, const char * name) const
{ return (builder.*op)(a, name); }
Value* operator()(op_no_name op, Value* const& a) const
{ return (builder.*op)(a); }
Value* operator()(short v) const { return ConstantInt::get(getGlobalContext(), APInt(sizeof(short)*8, v)); }
Value* operator()(int v) const { return ConstantInt::get(getGlobalContext(), APInt(sizeof(int)*8, v)); }
Value* operator()(long v) const { return ConstantInt::get(getGlobalContext(), APInt(sizeof(long)*8, v)); }
Value* operator()(float v) const { return ConstantFP::get(getGlobalContext(), APFloat(v)); }
Value* operator()(double v)const{ return ConstantFP::get(getGlobalContext(), APFloat(v)); }
Builder& builder;
};
language_1_grammar(char const * const name)
: language_1_grammar::base_type(module),
module_ptr(new Module(name, getGlobalContext()))
, build(*(new Builder(BasicBlock::Create(getGlobalContext(),"entry",
cast<Function>(module_ptr->getOrInsertFunction("main", type<value_t>(), NULL))))))
{
module = program[_val=module_ptr];
program = additive_expression [_val=build(&Builder::CreateRet,_1)]
;
additive_expression =
multiplicative_expression [_val=_1]
>> *(
('+' >> multiplicative_expression
[_val= build(&Builder::CreateAdd, _val, _1,"addtmp")])
|('-' >> multiplicative_expression
[_val=build(&Builder::CreateSub, _val, _1,"subtmp")])
)
;
multiplicative_expression =
factor [_val=_1]
>> *(
('*' >> factor[_val= build(&Builder::CreateMul, _val, _1, "multmp")])
|('/' >> factor [_val = build(&Builder::CreateSDiv, _val, _1, "divtmp")])
)
;
factor =
numeric_to_val [_val=_1]
| '(' >> additive_expression [_val=_1] >> ')'
| ('-' >> factor
[_val= build(&Builder::CreateNeg, _1,"negtmp")]
)
| ('+' >> factor [_val=_1])
;
numeric_to_val = boost::spirit::traits::create_parser<value_t>::call()[_val=build(_1)];
}
rule<Iterator, Value*(), boost::spirit::standard::space_type> additive_expression, multiplicative_expression
, factor, numeric_to_val, program;
rule<Iterator, Module*(), boost::spirit::standard::space_type> module;
Module* module_ptr;
boost::phoenix::function<builder_helper> build;
};
int main(int argc, char* argv[]){
typedef int value_t; // value used in arithmetic computations
if(argc > 2 )
{ std::cerr<< "Usage: "<<argv[0]<<" [nb of executions]\n"; return 1;}
std::cin.unsetf(std::ios::skipws); // Turn off white space skipping on the stream
typedef std::string buffer_t;
buffer_t buffer(std::istream_iterator<char>(std::cin), (std::istream_iterator<char>()));
typedef buffer_t::const_iterator iter_t;
iter_t iter(buffer.begin()), end(buffer.end());
typedef language_1_grammar<value_t, iter_t, llvm::IRBuilder<true, NoFolder> > grammar_t;
Module* module_ptr(0);
grammar_t grammar("lang_1");
bool r = phrase_parse(iter, end, grammar, boost::spirit::standard::space, module_ptr);
if (r && iter == end) {
std::cout<<"parsing succeded !\n";
verifyModule(*module_ptr, PrintMessageAction);
module_ptr->dump();
llvm::InitializeNativeTarget();
std::string ErrStr;
llvm::ExecutionEngine* exec_engine_ptr= llvm::EngineBuilder(module_ptr).setErrorStr(&ErrStr).create();
if (!exec_engine_ptr) {
std::cerr<<"Could not create ExecutionEngine:"<< ErrStr.c_str()<<std::endl;
}else {
typedef value_t (*fun_ptr_t)(void);
fun_ptr_t fun_ptr =
reinterpret_cast<fun_ptr_t>(exec_engine_ptr->getPointerToFunction(module_ptr->getFunction("main")));
std::cout<<"result: "<<(*fun_ptr)()<<std::endl;
}
} else {
std::string rest(iter, end);
std::cerr << "parsing failed\n" << "stopped at: \"" << rest << "\"\n";
}
delete module_ptr;
return r ? 0 : 1 ;
}