14899
|
1 /* |
|
2 |
|
3 Copyright (C) 2009-2012 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_tree_jit_h) |
|
24 #define octave_tree_jit_h 1 |
|
25 |
|
26 #include <map> |
|
27 #include <stdexcept> |
|
28 #include <vector> |
|
29 |
|
30 #include "pt-walk.h" |
|
31 |
|
32 class jit_fail_exception : public std::exception {}; |
|
33 |
|
34 // LLVM forward declares |
|
35 namespace llvm |
|
36 { |
|
37 class Value; |
|
38 class Module; |
|
39 class FunctionPassManager; |
|
40 class ExecutionEngine; |
|
41 class Function; |
|
42 class BasicBlock; |
|
43 class LLVMContext; |
|
44 } |
|
45 |
|
46 class tree; |
|
47 |
|
48 class |
|
49 OCTINTERP_API |
|
50 tree_jit : private tree_walker |
|
51 { |
|
52 public: |
|
53 tree_jit (void); |
|
54 |
|
55 ~tree_jit (void); |
|
56 |
|
57 bool execute (tree& tee); |
|
58 private: |
|
59 typedef void (*jit_function)(bool*, double*); |
|
60 |
|
61 class function_info |
|
62 { |
|
63 public: |
|
64 function_info (void); |
|
65 function_info (jit_function fn, const std::vector<std::string>& args, |
|
66 const std::vector<bool>& args_used); |
|
67 |
|
68 bool execute (); |
|
69 private: |
|
70 jit_function function; |
|
71 std::vector<std::string> arguments; |
|
72 |
|
73 // is the argument used? or is it just declared? |
|
74 std::vector<bool> argument_used; |
|
75 }; |
|
76 |
|
77 struct variable_info |
|
78 { |
|
79 llvm::Value *defined; |
|
80 llvm::Value *value; |
|
81 bool use; |
|
82 }; |
|
83 |
|
84 function_info *compile (tree& tee); |
|
85 |
|
86 variable_info find (const std::string &name, bool use); |
|
87 |
|
88 void do_assign (variable_info vinfo, llvm::Value *value); |
|
89 |
|
90 void emit_print (const std::string& vname, llvm::Value *value); |
|
91 |
|
92 // tree_walker |
|
93 void visit_anon_fcn_handle (tree_anon_fcn_handle&); |
|
94 |
|
95 void visit_argument_list (tree_argument_list&); |
|
96 |
|
97 void visit_binary_expression (tree_binary_expression&); |
|
98 |
|
99 void visit_break_command (tree_break_command&); |
|
100 |
|
101 void visit_colon_expression (tree_colon_expression&); |
|
102 |
|
103 void visit_continue_command (tree_continue_command&); |
|
104 |
|
105 void visit_global_command (tree_global_command&); |
|
106 |
|
107 void visit_persistent_command (tree_persistent_command&); |
|
108 |
|
109 void visit_decl_elt (tree_decl_elt&); |
|
110 |
|
111 void visit_decl_init_list (tree_decl_init_list&); |
|
112 |
|
113 void visit_simple_for_command (tree_simple_for_command&); |
|
114 |
|
115 void visit_complex_for_command (tree_complex_for_command&); |
|
116 |
|
117 void visit_octave_user_script (octave_user_script&); |
|
118 |
|
119 void visit_octave_user_function (octave_user_function&); |
|
120 |
|
121 void visit_octave_user_function_header (octave_user_function&); |
|
122 |
|
123 void visit_octave_user_function_trailer (octave_user_function&); |
|
124 |
|
125 void visit_function_def (tree_function_def&); |
|
126 |
|
127 void visit_identifier (tree_identifier&); |
|
128 |
|
129 void visit_if_clause (tree_if_clause&); |
|
130 |
|
131 void visit_if_command (tree_if_command&); |
|
132 |
|
133 void visit_if_command_list (tree_if_command_list&); |
|
134 |
|
135 void visit_index_expression (tree_index_expression&); |
|
136 |
|
137 void visit_matrix (tree_matrix&); |
|
138 |
|
139 void visit_cell (tree_cell&); |
|
140 |
|
141 void visit_multi_assignment (tree_multi_assignment&); |
|
142 |
|
143 void visit_no_op_command (tree_no_op_command&); |
|
144 |
|
145 void visit_constant (tree_constant&); |
|
146 |
|
147 void visit_fcn_handle (tree_fcn_handle&); |
|
148 |
|
149 void visit_parameter_list (tree_parameter_list&); |
|
150 |
|
151 void visit_postfix_expression (tree_postfix_expression&); |
|
152 |
|
153 void visit_prefix_expression (tree_prefix_expression&); |
|
154 |
|
155 void visit_return_command (tree_return_command&); |
|
156 |
|
157 void visit_return_list (tree_return_list&); |
|
158 |
|
159 void visit_simple_assignment (tree_simple_assignment&); |
|
160 |
|
161 void visit_statement (tree_statement&); |
|
162 |
|
163 void visit_statement_list (tree_statement_list&); |
|
164 |
|
165 void visit_switch_case (tree_switch_case&); |
|
166 |
|
167 void visit_switch_case_list (tree_switch_case_list&); |
|
168 |
|
169 void visit_switch_command (tree_switch_command&); |
|
170 |
|
171 void visit_try_catch_command (tree_try_catch_command&); |
|
172 |
|
173 void do_unwind_protect_cleanup_code (tree_statement_list *list); |
|
174 |
|
175 void visit_unwind_protect_command (tree_unwind_protect_command&); |
|
176 |
|
177 void visit_while_command (tree_while_command&); |
|
178 |
|
179 void visit_do_until_command (tree_do_until_command&); |
|
180 |
|
181 void fail (void); |
|
182 |
|
183 typedef std::map<std::string, variable_info> var_map; |
|
184 typedef var_map::iterator var_map_iterator; |
|
185 typedef std::map<tree*, function_info*> finfo_map; |
|
186 typedef finfo_map::iterator finfo_map_iterator; |
|
187 |
|
188 std::vector<llvm::Value*> value_stack; |
|
189 var_map variables; |
|
190 finfo_map compiled_functions; |
|
191 |
|
192 llvm::LLVMContext &context; |
|
193 llvm::Module *module; |
|
194 llvm::FunctionPassManager *pass_manager; |
|
195 llvm::ExecutionEngine *engine; |
|
196 llvm::BasicBlock *entry_block; |
|
197 |
|
198 llvm::Function *print_double; |
|
199 }; |
|
200 |
|
201 #endif |