jit_compiler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _JIT_COMPILER_H_
  6. #define _JIT_COMPILER_H_
  7. #include "bh_platform.h"
  8. #include "../interpreter/wasm_runtime.h"
  9. #include "jit_ir.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef struct JitGlobals {
  14. /* Compiler pass sequence, the last element must be 0 */
  15. const uint8 *passes;
  16. char *return_to_interp_from_jitted;
  17. #if WASM_ENABLE_LAZY_JIT != 0
  18. char *compile_fast_jit_and_then_call;
  19. #endif
  20. } JitGlobals;
  21. /**
  22. * Actions the interpreter should do when jitted code returns to
  23. * interpreter.
  24. */
  25. typedef enum JitInterpAction {
  26. JIT_INTERP_ACTION_NORMAL, /* normal execution */
  27. JIT_INTERP_ACTION_THROWN, /* exception was thrown */
  28. JIT_INTERP_ACTION_CALL /* call wasm function */
  29. } JitInterpAction;
  30. /**
  31. * Information exchanged between jitted code and interpreter.
  32. */
  33. typedef struct JitInterpSwitchInfo {
  34. /* Points to the frame that is passed to jitted code and the frame
  35. that is returned from jitted code */
  36. void *frame;
  37. /* Output values from jitted code of different actions */
  38. union {
  39. /* IP and SP offsets for NORMAL */
  40. struct {
  41. int32 ip;
  42. int32 sp;
  43. } normal;
  44. /* Function called from jitted code for CALL */
  45. struct {
  46. void *function;
  47. } call;
  48. /* Returned integer and/or floating point values for RETURN. This
  49. is also used to pass return values from interpreter to jitted
  50. code if the caller is in jitted code and the callee is in
  51. interpreter. */
  52. struct {
  53. uint32 ival[2];
  54. uint32 fval[2];
  55. uint32 last_return_type;
  56. } ret;
  57. } out;
  58. } JitInterpSwitchInfo;
  59. /* Jit compiler options */
  60. typedef struct JitCompOptions {
  61. uint32 code_cache_size;
  62. uint32 opt_level;
  63. } JitCompOptions;
  64. bool
  65. jit_compiler_init(const JitCompOptions *option);
  66. void
  67. jit_compiler_destroy();
  68. JitGlobals *
  69. jit_compiler_get_jit_globals();
  70. const char *
  71. jit_compiler_get_pass_name(unsigned i);
  72. bool
  73. jit_compiler_compile(WASMModule *module, uint32 func_idx);
  74. bool
  75. jit_compiler_compile_all(WASMModule *module);
  76. bool
  77. jit_compiler_is_compiled(const WASMModule *module, uint32 func_idx);
  78. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_JIT != 0
  79. bool
  80. jit_compiler_set_call_to_llvm_jit(WASMModule *module, uint32 func_idx);
  81. bool
  82. jit_compiler_set_call_to_fast_jit(WASMModule *module, uint32 func_idx);
  83. void
  84. jit_compiler_set_llvm_jit_func_ptr(WASMModule *module, uint32 func_idx,
  85. void *func_ptr);
  86. #endif
  87. int
  88. jit_interp_switch_to_jitted(void *self, JitInterpSwitchInfo *info,
  89. uint32 func_idx, void *pc);
  90. /*
  91. * Pass declarations:
  92. */
  93. /**
  94. * Dump the compilation context.
  95. */
  96. bool
  97. jit_pass_dump(JitCompContext *cc);
  98. /**
  99. * Update CFG (usually before dump for better readability).
  100. */
  101. bool
  102. jit_pass_update_cfg(JitCompContext *cc);
  103. /**
  104. * Translate profiling result into MIR.
  105. */
  106. bool
  107. jit_pass_frontend(JitCompContext *cc);
  108. /**
  109. * Lower unsupported operations into supported ones.
  110. */
  111. bool
  112. jit_pass_lower_cg(JitCompContext *cc);
  113. /**
  114. * Register allocation.
  115. */
  116. bool
  117. jit_pass_regalloc(JitCompContext *cc);
  118. /**
  119. * Native code generation.
  120. */
  121. bool
  122. jit_pass_codegen(JitCompContext *cc);
  123. /**
  124. * Register the jitted code so that it can be executed.
  125. */
  126. bool
  127. jit_pass_register_jitted_code(JitCompContext *cc);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif /* end of _JIT_COMPILER_H_ */