compiler.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #if !defined(BOOST_SPIRIT_CONJURE_COMPILER_HPP)
  7. #define BOOST_SPIRIT_CONJURE_COMPILER_HPP
  8. #include "ast.hpp"
  9. #include "error_handler.hpp"
  10. #include "vm.hpp"
  11. #include <map>
  12. #include <boost/function.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/spirit/include/phoenix_core.hpp>
  15. #include <boost/spirit/include/phoenix_function.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. #include <boost/iterator/transform_iterator.hpp>
  18. #include <llvm/DerivedTypes.h>
  19. #include <llvm/Constants.h>
  20. #include <llvm/LLVMContext.h>
  21. #include <llvm/Module.h>
  22. #include <llvm/PassManager.h>
  23. #include <llvm/Analysis/Verifier.h>
  24. #include <llvm/Analysis/Passes.h>
  25. #include <llvm/Transforms/Scalar.h>
  26. #include <llvm/Support/IRBuilder.h>
  27. namespace client { namespace code_gen
  28. {
  29. unsigned const int_size = 32;
  30. struct compiler;
  31. struct llvm_compiler;
  32. ///////////////////////////////////////////////////////////////////////////
  33. // The Value (light abstraction of an LLVM::Value)
  34. ///////////////////////////////////////////////////////////////////////////
  35. struct value
  36. {
  37. value();
  38. value(value const& rhs);
  39. value& operator=(value const& rhs);
  40. bool is_lvalue() const;
  41. bool is_valid() const;
  42. operator bool() const;
  43. value& assign(value const& rhs);
  44. void name(char const* id);
  45. void name(std::string const& id);
  46. friend value operator-(value a);
  47. friend value operator!(value a);
  48. friend value operator+(value a, value b);
  49. friend value operator-(value a, value b);
  50. friend value operator*(value a, value b);
  51. friend value operator/(value a, value b);
  52. friend value operator%(value a, value b);
  53. friend value operator&(value a, value b);
  54. friend value operator|(value a, value b);
  55. friend value operator^(value a, value b);
  56. friend value operator<<(value a, value b);
  57. friend value operator>>(value a, value b);
  58. friend value operator==(value a, value b);
  59. friend value operator!=(value a, value b);
  60. friend value operator<(value a, value b);
  61. friend value operator<=(value a, value b);
  62. friend value operator>(value a, value b);
  63. friend value operator>=(value a, value b);
  64. private:
  65. struct to_llvm_value;
  66. friend struct to_llvm_value;
  67. friend struct llvm_compiler;
  68. value(
  69. llvm::Value* v,
  70. bool is_lvalue_,
  71. llvm::IRBuilder<>* builder);
  72. llvm::LLVMContext& context() const
  73. { return llvm::getGlobalContext(); }
  74. operator llvm::Value*() const;
  75. llvm::Value* v;
  76. bool is_lvalue_;
  77. llvm::IRBuilder<>* builder;
  78. };
  79. ///////////////////////////////////////////////////////////////////////////
  80. // The Basic Block (light abstraction of an LLVM::BasicBlock)
  81. ///////////////////////////////////////////////////////////////////////////
  82. struct function;
  83. struct basic_block
  84. {
  85. basic_block()
  86. : b(0) {}
  87. bool has_terminator() const;
  88. bool is_valid() const;
  89. private:
  90. basic_block(llvm::BasicBlock* b)
  91. : b(b) {}
  92. operator llvm::BasicBlock*() const
  93. { return b; }
  94. friend struct llvm_compiler;
  95. friend struct function;
  96. llvm::BasicBlock* b;
  97. };
  98. ///////////////////////////////////////////////////////////////////////////
  99. // The Function (light abstraction of an LLVM::Function)
  100. ///////////////////////////////////////////////////////////////////////////
  101. struct llvm_compiler;
  102. struct function
  103. {
  104. private:
  105. struct to_value;
  106. typedef llvm::Function::arg_iterator arg_iterator;
  107. typedef boost::transform_iterator<
  108. to_value, arg_iterator>
  109. arg_val_iterator;
  110. public:
  111. typedef boost::iterator_range<arg_val_iterator> arg_range;
  112. function()
  113. : f(0), c(c) {}
  114. std::string name() const;
  115. std::size_t arg_size() const;
  116. arg_range args() const;
  117. void add(basic_block const& b);
  118. void erase_from_parent();
  119. basic_block last_block();
  120. bool empty() const;
  121. bool is_valid() const;
  122. void verify() const;
  123. private:
  124. function(llvm::Function* f, llvm_compiler* c)
  125. : f(f), c(c) {}
  126. operator llvm::Function*() const;
  127. friend struct llvm_compiler;
  128. llvm::Function* f;
  129. llvm_compiler* c;
  130. };
  131. ///////////////////////////////////////////////////////////////////////////
  132. // The LLVM Compiler. Lower level compiler (does not deal with ASTs)
  133. ///////////////////////////////////////////////////////////////////////////
  134. struct llvm_compiler
  135. {
  136. llvm_compiler(vmachine& vm)
  137. : llvm_builder(context())
  138. , vm(vm)
  139. , fpm(vm.module())
  140. { init_fpm(); }
  141. value val() { return value(); }
  142. value val(unsigned int x);
  143. value val(int x);
  144. value val(bool x);
  145. value var(char const* name);
  146. value var(std::string const& name);
  147. template <typename Container>
  148. value call(function callee, Container const& args);
  149. function get_function(char const* name);
  150. function get_function(std::string const& name);
  151. function get_current_function();
  152. function declare_function(
  153. bool void_return
  154. , std::string const& name
  155. , std::size_t nargs);
  156. basic_block make_basic_block(
  157. char const* name
  158. , function parent = function()
  159. , basic_block before = basic_block());
  160. basic_block get_insert_block();
  161. void set_insert_point(basic_block b);
  162. void conditional_branch(
  163. value cond, basic_block true_br, basic_block false_br);
  164. void branch(basic_block b);
  165. void return_();
  166. void return_(value v);
  167. void optimize_function(function f);
  168. protected:
  169. llvm::LLVMContext& context() const
  170. { return llvm::getGlobalContext(); }
  171. llvm::IRBuilder<>& builder()
  172. { return llvm_builder; }
  173. private:
  174. friend struct function::to_value;
  175. value val(llvm::Value* v);
  176. template <typename C>
  177. llvm::Value* call_impl(
  178. function callee,
  179. C const& args);
  180. void init_fpm();
  181. llvm::IRBuilder<> llvm_builder;
  182. vmachine& vm;
  183. llvm::FunctionPassManager fpm;
  184. };
  185. ///////////////////////////////////////////////////////////////////////////
  186. // The main compiler. Generates code from our AST.
  187. ///////////////////////////////////////////////////////////////////////////
  188. struct compiler : llvm_compiler
  189. {
  190. typedef value result_type;
  191. template <typename ErrorHandler>
  192. compiler(vmachine& vm, ErrorHandler& error_handler_)
  193. : llvm_compiler(vm)
  194. {
  195. using namespace boost::phoenix::arg_names;
  196. namespace phx = boost::phoenix;
  197. using boost::phoenix::function;
  198. error_handler = function<ErrorHandler>(error_handler_)(
  199. "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
  200. }
  201. value operator()(ast::nil) { BOOST_ASSERT(0); return val(); }
  202. value operator()(unsigned int x);
  203. value operator()(bool x);
  204. value operator()(ast::primary_expr const& x);
  205. value operator()(ast::identifier const& x);
  206. value operator()(ast::unary_expr const& x);
  207. value operator()(ast::function_call const& x);
  208. value operator()(ast::expression const& x);
  209. value operator()(ast::assignment const& x);
  210. bool operator()(ast::variable_declaration const& x);
  211. bool operator()(ast::statement_list const& x);
  212. bool operator()(ast::statement const& x);
  213. bool operator()(ast::if_statement const& x);
  214. bool operator()(ast::while_statement const& x);
  215. bool operator()(ast::return_statement const& x);
  216. bool operator()(ast::function const& x);
  217. bool operator()(ast::function_list const& x);
  218. private:
  219. value compile_binary_expression(
  220. value lhs, value rhs, token_ids::type op);
  221. value compile_expression(
  222. int min_precedence,
  223. value lhs,
  224. std::list<ast::operation>::const_iterator& rest_begin,
  225. std::list<ast::operation>::const_iterator rest_end);
  226. struct statement_compiler;
  227. statement_compiler& as_statement();
  228. function function_decl(ast::function const& x);
  229. void function_allocas(ast::function const& x, function function);
  230. boost::function<
  231. void(int tag, std::string const& what)>
  232. error_handler;
  233. bool void_return;
  234. std::string current_function_name;
  235. std::map<std::string, value> locals;
  236. basic_block return_block;
  237. value return_var;
  238. };
  239. }}
  240. #endif