github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/include/jit/jit-plus.h (about) 1 /* 2 * jit-plus.h - C++ binding for the JIT library. 3 * 4 * Copyright (C) 2004 Southern Storm Software, Pty Ltd. 5 * 6 * The libjit library is free software: you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public License 8 * as published by the Free Software Foundation, either version 2.1 of 9 * the License, or (at your option) any later version. 10 * 11 * The libjit library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with the libjit library. If not, see 18 * <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef _JIT_PLUS_H 22 #define _JIT_PLUS_H 23 24 #include <jit/jit.h> 25 26 #ifdef __cplusplus 27 28 class jit_build_exception 29 { 30 public: 31 jit_build_exception(int result) { this->result = result; } 32 ~jit_build_exception() {} 33 34 int result; 35 }; 36 37 class jit_value 38 { 39 public: 40 jit_value() { this->value = 0; } 41 jit_value(jit_value_t value) { this->value = value; } 42 jit_value(const jit_value& value) { this->value = value.value; } 43 ~jit_value() {} 44 45 jit_value& operator=(const jit_value& value) 46 { this->value = value.value; return *this; } 47 48 jit_value_t raw() const { return value; } 49 int is_valid() const { return (value != 0); } 50 51 int is_temporary() const { return jit_value_is_temporary(value); } 52 int is_local() const { return jit_value_is_local(value); } 53 int is_constant() const { return jit_value_is_constant(value); } 54 int is_parameter() const { return jit_value_is_parameter(value); } 55 56 void set_volatile() { jit_value_set_volatile(value); } 57 int is_volatile() const { return jit_value_is_volatile(value); } 58 59 void set_addressable() { jit_value_set_addressable(value); } 60 int is_addressable() const { return jit_value_is_addressable(value); } 61 62 jit_type_t type() const { return jit_value_get_type(value); } 63 jit_function_t function() const { return jit_value_get_function(value); } 64 jit_block_t block() const { return jit_value_get_block(value); } 65 jit_context_t context() const { return jit_value_get_context(value); } 66 67 jit_constant_t constant() const 68 { return jit_value_get_constant(value); } 69 jit_nint nint_constant() const 70 { return jit_value_get_nint_constant(value); } 71 jit_long long_constant() const 72 { return jit_value_get_long_constant(value); } 73 jit_float32 float32_constant() const 74 { return jit_value_get_float32_constant(value); } 75 jit_float64 float64_constant() const 76 { return jit_value_get_float64_constant(value); } 77 jit_nfloat nfloat_constant() const 78 { return jit_value_get_nfloat_constant(value); } 79 80 private: 81 jit_value_t value; 82 }; 83 84 jit_value operator+(const jit_value& value1, const jit_value& value2); 85 jit_value operator-(const jit_value& value1, const jit_value& value2); 86 jit_value operator*(const jit_value& value1, const jit_value& value2); 87 jit_value operator/(const jit_value& value1, const jit_value& value2); 88 jit_value operator%(const jit_value& value1, const jit_value& value2); 89 jit_value operator-(const jit_value& value1); 90 jit_value operator&(const jit_value& value1, const jit_value& value2); 91 jit_value operator|(const jit_value& value1, const jit_value& value2); 92 jit_value operator^(const jit_value& value1, const jit_value& value2); 93 jit_value operator~(const jit_value& value1); 94 jit_value operator<<(const jit_value& value1, const jit_value& value2); 95 jit_value operator>>(const jit_value& value1, const jit_value& value2); 96 jit_value operator==(const jit_value& value1, const jit_value& value2); 97 jit_value operator!=(const jit_value& value1, const jit_value& value2); 98 jit_value operator<(const jit_value& value1, const jit_value& value2); 99 jit_value operator<=(const jit_value& value1, const jit_value& value2); 100 jit_value operator>(const jit_value& value1, const jit_value& value2); 101 jit_value operator>=(const jit_value& value1, const jit_value& value2); 102 103 class jit_label 104 { 105 public: 106 jit_label() { label = jit_label_undefined; } 107 jit_label(jit_label_t label) { this->label = label; } 108 jit_label(const jit_label& label) { this->label = label.label; } 109 ~jit_label() {} 110 111 jit_label_t raw() const { return label; } 112 jit_label_t *rawp() { return &label; } 113 int is_valid() const { return (label != jit_label_undefined); } 114 115 jit_label& operator=(const jit_label& value) 116 { this->label = value.label; return *this; } 117 118 private: 119 jit_label_t label; 120 }; 121 122 class jit_jump_table 123 { 124 public: 125 126 jit_jump_table(int size); 127 ~jit_jump_table(); 128 129 int size() { return num_labels; } 130 jit_label_t *raw() { return labels; } 131 132 jit_label get(int index); 133 134 void set(int index, jit_label label); 135 136 private: 137 138 jit_label_t *labels; 139 int num_labels; 140 141 // forbid copying 142 jit_jump_table(const jit_jump_table&); 143 jit_jump_table& operator=(const jit_jump_table&); 144 }; 145 146 class jit_context 147 { 148 public: 149 jit_context(); 150 jit_context(jit_context_t context); 151 ~jit_context(); 152 153 void build_start() { jit_context_build_start(context); } 154 void build_end() { jit_context_build_end(context); } 155 jit_context_t raw() const { return context; } 156 157 private: 158 jit_context_t context; 159 int copied; 160 }; 161 162 class jit_function 163 { 164 public: 165 jit_function(jit_context& context, jit_type_t signature); 166 jit_function(jit_context& context); 167 jit_function(jit_function_t func); 168 virtual ~jit_function(); 169 170 jit_function_t raw() const { return func; } 171 int is_valid() const { return (func != 0); } 172 173 static jit_function *from_raw(jit_function_t func); 174 175 jit_type_t signature() const { return jit_function_get_signature(func); } 176 177 void create(jit_type_t signature); 178 void create(); 179 180 int compile(); 181 182 int is_compiled() const { return jit_function_is_compiled(func); } 183 184 int is_recompilable() const { return jit_function_is_recompilable(func); } 185 186 void set_recompilable() { jit_function_set_recompilable(func); } 187 void clear_recompilable() { jit_function_clear_recompilable(func); } 188 void set_recompilable(int flag) 189 { if(flag) set_recompilable(); else clear_recompilable(); } 190 191 void set_optimization_level(unsigned int level) 192 { jit_function_set_optimization_level(func, level); } 193 unsigned int optimization_level() const 194 { return jit_function_get_optimization_level(func); } 195 static unsigned int max_optimization_level() 196 { return jit_function_get_max_optimization_level(); } 197 198 void *closure() const { return jit_function_to_closure(func); } 199 void *vtable_pointer() const 200 { return jit_function_to_vtable_pointer(func); } 201 202 int apply(void **args, void *result) 203 { return jit_function_apply(func, args, result); } 204 int apply(jit_type_t signature, void **args, void *return_area) 205 { return jit_function_apply_vararg 206 (func, signature, args, return_area); } 207 208 static jit_type_t const end_params; 209 static jit_type_t signature_helper(jit_type_t return_type, ...); 210 211 protected: 212 virtual void build(); 213 virtual jit_type_t create_signature(); 214 void fail(); 215 void out_of_memory(); 216 217 public: 218 void build_start() 219 { jit_context_build_start(jit_function_get_context(func)); } 220 void build_end() 221 { jit_context_build_end(jit_function_get_context(func)); } 222 223 jit_value new_value(jit_type_t type); 224 jit_value new_constant(jit_sbyte value, jit_type_t type=0); 225 jit_value new_constant(jit_ubyte value, jit_type_t type=0); 226 jit_value new_constant(jit_short value, jit_type_t type=0); 227 jit_value new_constant(jit_ushort value, jit_type_t type=0); 228 jit_value new_constant(jit_int value, jit_type_t type=0); 229 jit_value new_constant(jit_uint value, jit_type_t type=0); 230 jit_value new_constant(jit_long value, jit_type_t type=0); 231 jit_value new_constant(jit_ulong value, jit_type_t type=0); 232 jit_value new_constant(jit_float32 value, jit_type_t type=0); 233 jit_value new_constant(jit_float64 value, jit_type_t type=0); 234 #ifndef JIT_NFLOAT_IS_DOUBLE 235 jit_value new_constant(jit_nfloat value, jit_type_t type=0); 236 #endif 237 jit_value new_constant(void *value, jit_type_t type=0); 238 jit_value new_constant(const jit_constant_t& value); 239 jit_value get_param(unsigned int param); 240 jit_value get_struct_pointer(); 241 242 jit_label new_label(); 243 244 void insn_label(jit_label& label); 245 void insn_label_tight(jit_label& label); 246 247 void insn_new_block(); 248 249 jit_value insn_load(const jit_value& value); 250 jit_value insn_dup(const jit_value& value); 251 void store(const jit_value& dest, const jit_value& value); 252 jit_value insn_load_relative 253 (const jit_value& value, jit_nint offset, jit_type_t type); 254 void insn_store_relative 255 (const jit_value& dest, jit_nint offset, const jit_value& value); 256 jit_value insn_add_relative(const jit_value& value, jit_nint offset); 257 jit_value insn_load_elem 258 (const jit_value& base_addr, const jit_value& index, 259 jit_type_t elem_type); 260 jit_value insn_load_elem_address 261 (const jit_value& base_addr, const jit_value& index, 262 jit_type_t elem_type); 263 void insn_store_elem 264 (const jit_value& base_addr, const jit_value& index, 265 const jit_value& value); 266 void insn_check_null(const jit_value& value); 267 jit_value insn_add(const jit_value& value1, const jit_value& value2); 268 jit_value insn_add_ovf(const jit_value& value1, const jit_value& value2); 269 jit_value insn_sub(const jit_value& value1, const jit_value& value2); 270 jit_value insn_sub_ovf(const jit_value& value1, const jit_value& value2); 271 jit_value insn_mul(const jit_value& value1, const jit_value& value2); 272 jit_value insn_mul_ovf(const jit_value& value1, const jit_value& value2); 273 jit_value insn_div(const jit_value& value1, const jit_value& value2); 274 jit_value insn_rem(const jit_value& value1, const jit_value& value2); 275 jit_value insn_rem_ieee(const jit_value& value1, const jit_value& value2); 276 jit_value insn_neg(const jit_value& value1); 277 jit_value insn_and(const jit_value& value1, const jit_value& value2); 278 jit_value insn_or(const jit_value& value1, const jit_value& value2); 279 jit_value insn_xor(const jit_value& value1, const jit_value& value2); 280 jit_value insn_not(const jit_value& value1); 281 jit_value insn_shl(const jit_value& value1, const jit_value& value2); 282 jit_value insn_shr(const jit_value& value1, const jit_value& value2); 283 jit_value insn_ushr(const jit_value& value1, const jit_value& value2); 284 jit_value insn_sshr(const jit_value& value1, const jit_value& value2); 285 jit_value insn_eq(const jit_value& value1, const jit_value& value2); 286 jit_value insn_ne(const jit_value& value1, const jit_value& value2); 287 jit_value insn_lt(const jit_value& value1, const jit_value& value2); 288 jit_value insn_le(const jit_value& value1, const jit_value& value2); 289 jit_value insn_gt(const jit_value& value1, const jit_value& value2); 290 jit_value insn_ge(const jit_value& value1, const jit_value& value2); 291 jit_value insn_cmpl(const jit_value& value1, const jit_value& value2); 292 jit_value insn_cmpg(const jit_value& value1, const jit_value& value2); 293 jit_value insn_to_bool(const jit_value& value1); 294 jit_value insn_to_not_bool(const jit_value& value1); 295 jit_value insn_acos(const jit_value& value1); 296 jit_value insn_asin(const jit_value& value1); 297 jit_value insn_atan(const jit_value& value1); 298 jit_value insn_atan2(const jit_value& value1, const jit_value& value2); 299 jit_value insn_ceil(const jit_value& value1); 300 jit_value insn_cos(const jit_value& value1); 301 jit_value insn_cosh(const jit_value& value1); 302 jit_value insn_exp(const jit_value& value1); 303 jit_value insn_floor(const jit_value& value1); 304 jit_value insn_log(const jit_value& value1); 305 jit_value insn_log10(const jit_value& value1); 306 jit_value insn_pow(const jit_value& value1, const jit_value& value2); 307 jit_value insn_rint(const jit_value& value1); 308 jit_value insn_round(const jit_value& value1); 309 jit_value insn_sin(const jit_value& value1); 310 jit_value insn_sinh(const jit_value& value1); 311 jit_value insn_sqrt(const jit_value& value1); 312 jit_value insn_tan(const jit_value& value1); 313 jit_value insn_tanh(const jit_value& value1); 314 jit_value insn_trunc(const jit_value& value1); 315 jit_value insn_is_nan(const jit_value& value1); 316 jit_value insn_is_finite(const jit_value& value1); 317 jit_value insn_is_inf(const jit_value& value1); 318 jit_value insn_abs(const jit_value& value1); 319 jit_value insn_min(const jit_value& value1, const jit_value& value2); 320 jit_value insn_max(const jit_value& value1, const jit_value& value2); 321 jit_value insn_sign(const jit_value& value1); 322 void insn_branch(jit_label& label); 323 void insn_branch_if(const jit_value& value, jit_label& label); 324 void insn_branch_if_not(const jit_value& value, jit_label& label); 325 void insn_jump_table(const jit_value& value, jit_jump_table& jump_table); 326 jit_value insn_address_of(const jit_value& value1); 327 jit_value insn_address_of_label(jit_label& label); 328 jit_value insn_convert 329 (const jit_value& value, jit_type_t type, int overflow_check=0); 330 jit_value insn_call 331 (const char *name, jit_function_t jit_func, 332 jit_type_t signature, jit_value_t *args, unsigned int num_args, 333 int flags=0); 334 jit_value insn_call_indirect 335 (const jit_value& value, jit_type_t signature, 336 jit_value_t *args, unsigned int num_args, int flags=0); 337 jit_value insn_call_indirect_vtable 338 (const jit_value& value, jit_type_t signature, 339 jit_value_t *args, unsigned int num_args, int flags=0); 340 jit_value insn_call_native 341 (const char *name, void *native_func, jit_type_t signature, 342 jit_value_t *args, unsigned int num_args, int flags=0); 343 jit_value insn_call_intrinsic 344 (const char *name, void *intrinsic_func, 345 const jit_intrinsic_descr_t& descriptor, 346 const jit_value& arg1, const jit_value& arg2); 347 void insn_incoming_reg(const jit_value& value, int reg); 348 void insn_incoming_frame_posn(const jit_value& value, jit_nint posn); 349 void insn_outgoing_reg(const jit_value& value, int reg); 350 void insn_outgoing_frame_posn(const jit_value& value, jit_nint posn); 351 void insn_return_reg(const jit_value& value, int reg); 352 void insn_setup_for_nested(int nested_level, int reg); 353 void insn_flush_struct(const jit_value& value); 354 jit_value insn_import(jit_value value); 355 void insn_push(const jit_value& value); 356 void insn_push_ptr(const jit_value& value, jit_type_t type); 357 void insn_set_param(const jit_value& value, jit_nint offset); 358 void insn_set_param_ptr 359 (const jit_value& value, jit_type_t type, jit_nint offset); 360 void insn_push_return_area_ptr(); 361 void insn_return(const jit_value& value); 362 void insn_return(); 363 void insn_return_ptr(const jit_value& value, jit_type_t type); 364 void insn_default_return(); 365 void insn_throw(const jit_value& value); 366 jit_value insn_get_call_stack(); 367 jit_value insn_thrown_exception(); 368 void insn_uses_catcher(); 369 jit_value insn_start_catcher(); 370 void insn_branch_if_pc_not_in_range 371 (const jit_label& start_label, const jit_label& end_label, 372 jit_label& label); 373 void insn_rethrow_unhandled(); 374 void insn_start_finally(jit_label& label); 375 void insn_return_from_finally(); 376 void insn_call_finally(jit_label& label); 377 jit_value insn_start_filter(jit_label& label, jit_type_t type); 378 void insn_return_from_filter(const jit_value& value); 379 jit_value insn_call_filter 380 (jit_label& label, const jit_value& value, jit_type_t type); 381 void insn_memcpy 382 (const jit_value& dest, const jit_value& src, const jit_value& size); 383 void insn_memmove 384 (const jit_value& dest, const jit_value& src, const jit_value& size); 385 void insn_memset 386 (const jit_value& dest, const jit_value& value, const jit_value& size); 387 jit_value insn_alloca(const jit_value& size); 388 void insn_move_blocks_to_end 389 (const jit_label& from_label, const jit_label& to_label); 390 void insn_move_blocks_to_start 391 (const jit_label& from_label, const jit_label& to_label); 392 void insn_mark_offset(jit_int offset); 393 void insn_mark_breakpoint(jit_nint data1, jit_nint data2); 394 395 private: 396 jit_function_t func; 397 jit_context_t context; 398 399 void register_on_demand(); 400 static int on_demand_compiler(jit_function_t func); 401 static void free_mapping(void *data); 402 }; 403 404 #endif /* __cplusplus */ 405 406 #endif /* _JIT_PLUS_H */