gdscript_byte_codegen.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************/
  2. /* gdscript_byte_codegen.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GDSCRIPT_BYTE_CODEGEN
  31. #define GDSCRIPT_BYTE_CODEGEN
  32. #include "gdscript_codegen.h"
  33. class GDScriptByteCodeGenerator : public GDScriptCodeGenerator {
  34. bool ended = false;
  35. GDScriptFunction *function = nullptr;
  36. bool debug_stack = false;
  37. Vector<int> opcodes;
  38. List<Map<StringName, int>> stack_id_stack;
  39. Map<StringName, int> stack_identifiers;
  40. Map<StringName, int> local_constants;
  41. List<GDScriptFunction::StackDebug> stack_debug;
  42. List<Map<StringName, int>> block_identifier_stack;
  43. Map<StringName, int> block_identifiers;
  44. int current_stack_size = 0;
  45. int current_temporaries = 0;
  46. HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
  47. Map<StringName, int> name_map;
  48. #ifdef TOOLS_ENABLED
  49. Vector<StringName> named_globals;
  50. #endif
  51. int current_line = 0;
  52. int stack_max = 0;
  53. int call_max = 0;
  54. List<int> if_jmp_addrs; // List since this can be nested.
  55. List<int> for_jmp_addrs;
  56. List<int> while_jmp_addrs;
  57. List<int> continue_addrs;
  58. // Used to patch jumps with `and` and `or` operators with short-circuit.
  59. List<int> logic_op_jump_pos1;
  60. List<int> logic_op_jump_pos2;
  61. List<Address> ternary_result;
  62. List<int> ternary_jump_fail_pos;
  63. List<int> ternary_jump_skip_pos;
  64. List<List<int>> current_breaks_to_patch;
  65. List<List<int>> match_continues_to_patch;
  66. void add_stack_identifier(const StringName &p_id, int p_stackpos) {
  67. stack_identifiers[p_id] = p_stackpos;
  68. if (debug_stack) {
  69. block_identifiers[p_id] = p_stackpos;
  70. GDScriptFunction::StackDebug sd;
  71. sd.added = true;
  72. sd.line = current_line;
  73. sd.identifier = p_id;
  74. sd.pos = p_stackpos;
  75. stack_debug.push_back(sd);
  76. }
  77. }
  78. void push_stack_identifiers() {
  79. stack_id_stack.push_back(stack_identifiers);
  80. if (debug_stack) {
  81. block_identifier_stack.push_back(block_identifiers);
  82. block_identifiers.clear();
  83. }
  84. }
  85. void pop_stack_identifiers() {
  86. stack_identifiers = stack_id_stack.back()->get();
  87. current_stack_size = stack_identifiers.size() + current_temporaries;
  88. stack_id_stack.pop_back();
  89. if (debug_stack) {
  90. for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
  91. GDScriptFunction::StackDebug sd;
  92. sd.added = false;
  93. sd.identifier = E->key();
  94. sd.line = current_line;
  95. sd.pos = E->get();
  96. stack_debug.push_back(sd);
  97. }
  98. block_identifiers = block_identifier_stack.back()->get();
  99. block_identifier_stack.pop_back();
  100. }
  101. }
  102. int get_name_map_pos(const StringName &p_identifier) {
  103. int ret;
  104. if (!name_map.has(p_identifier)) {
  105. ret = name_map.size();
  106. name_map[p_identifier] = ret;
  107. } else {
  108. ret = name_map[p_identifier];
  109. }
  110. return ret;
  111. }
  112. int get_constant_pos(const Variant &p_constant) {
  113. if (constant_map.has(p_constant))
  114. return constant_map[p_constant];
  115. int pos = constant_map.size();
  116. constant_map[p_constant] = pos;
  117. return pos;
  118. }
  119. void alloc_stack(int p_level) {
  120. if (p_level >= stack_max)
  121. stack_max = p_level + 1;
  122. }
  123. void alloc_call(int p_params) {
  124. if (p_params >= call_max)
  125. call_max = p_params;
  126. }
  127. int increase_stack() {
  128. int top = current_stack_size++;
  129. alloc_stack(current_stack_size);
  130. return top;
  131. }
  132. int address_of(const Address &p_address) {
  133. switch (p_address.mode) {
  134. case Address::SELF:
  135. return GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS;
  136. case Address::CLASS:
  137. return GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS;
  138. case Address::MEMBER:
  139. return p_address.address | (GDScriptFunction::ADDR_TYPE_MEMBER << GDScriptFunction::ADDR_BITS);
  140. case Address::CLASS_CONSTANT:
  141. return p_address.address | (GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT << GDScriptFunction::ADDR_BITS);
  142. case Address::LOCAL_CONSTANT:
  143. case Address::CONSTANT:
  144. return p_address.address | (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  145. case Address::LOCAL_VARIABLE:
  146. case Address::TEMPORARY:
  147. case Address::FUNCTION_PARAMETER:
  148. return p_address.address | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  149. case Address::GLOBAL:
  150. return p_address.address | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  151. case Address::NAMED_GLOBAL:
  152. return p_address.address | (GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL << GDScriptFunction::ADDR_BITS);
  153. case Address::NIL:
  154. return GDScriptFunction::ADDR_TYPE_NIL << GDScriptFunction::ADDR_BITS;
  155. }
  156. return -1; // Unreachable.
  157. }
  158. void append(int code) {
  159. opcodes.push_back(code);
  160. }
  161. void append(const Address &p_address) {
  162. opcodes.push_back(address_of(p_address));
  163. }
  164. void append(const StringName &p_name) {
  165. opcodes.push_back(get_name_map_pos(p_name));
  166. }
  167. void patch_jump(int p_address) {
  168. opcodes.write[p_address] = opcodes.size();
  169. }
  170. public:
  171. virtual uint32_t add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) override;
  172. virtual uint32_t add_local(const StringName &p_name, const GDScriptDataType &p_type) override;
  173. virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) override;
  174. virtual uint32_t add_or_get_constant(const Variant &p_constant) override;
  175. virtual uint32_t add_or_get_name(const StringName &p_name) override;
  176. virtual uint32_t add_temporary() override;
  177. virtual void pop_temporary() override;
  178. virtual void start_parameters() override;
  179. virtual void end_parameters() override;
  180. virtual void start_block() override;
  181. virtual void end_block() override;
  182. virtual void write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) override;
  183. virtual GDScriptFunction *write_end() override;
  184. #ifdef DEBUG_ENABLED
  185. virtual void set_signature(const String &p_signature) override;
  186. #endif
  187. virtual void set_initial_line(int p_line) override;
  188. virtual void write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) override;
  189. virtual void write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) override;
  190. virtual void write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) override;
  191. virtual void write_and_left_operand(const Address &p_left_operand) override;
  192. virtual void write_and_right_operand(const Address &p_right_operand) override;
  193. virtual void write_end_and(const Address &p_target) override;
  194. virtual void write_or_left_operand(const Address &p_left_operand) override;
  195. virtual void write_or_right_operand(const Address &p_right_operand) override;
  196. virtual void write_end_or(const Address &p_target) override;
  197. virtual void write_start_ternary(const Address &p_target) override;
  198. virtual void write_ternary_condition(const Address &p_condition) override;
  199. virtual void write_ternary_true_expr(const Address &p_expr) override;
  200. virtual void write_ternary_false_expr(const Address &p_expr) override;
  201. virtual void write_end_ternary() override;
  202. virtual void write_set(const Address &p_target, const Address &p_index, const Address &p_source) override;
  203. virtual void write_get(const Address &p_target, const Address &p_index, const Address &p_source) override;
  204. virtual void write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) override;
  205. virtual void write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) override;
  206. virtual void write_set_member(const Address &p_value, const StringName &p_name) override;
  207. virtual void write_get_member(const Address &p_target, const StringName &p_name) override;
  208. virtual void write_assign(const Address &p_target, const Address &p_source) override;
  209. virtual void write_assign_true(const Address &p_target) override;
  210. virtual void write_assign_false(const Address &p_target) override;
  211. virtual void write_assign_default_parameter(const Address &p_dst, const Address &p_src) override;
  212. virtual void write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) override;
  213. virtual void write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
  214. virtual void write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
  215. virtual void write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
  216. virtual void write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) override;
  217. virtual void write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) override;
  218. virtual void write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) override;
  219. virtual void write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
  220. virtual void write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) override;
  221. virtual void write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) override;
  222. virtual void write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) override;
  223. virtual void write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) override;
  224. virtual void write_await(const Address &p_target, const Address &p_operand) override;
  225. virtual void write_if(const Address &p_condition) override;
  226. virtual void write_else() override;
  227. virtual void write_endif() override;
  228. virtual void write_for(const Address &p_variable, const Address &p_list) override;
  229. virtual void write_endfor() override;
  230. virtual void start_while_condition() override;
  231. virtual void write_while(const Address &p_condition) override;
  232. virtual void write_endwhile() override;
  233. virtual void start_match() override;
  234. virtual void start_match_branch() override;
  235. virtual void end_match() override;
  236. virtual void write_break() override;
  237. virtual void write_continue() override;
  238. virtual void write_continue_match() override;
  239. virtual void write_breakpoint() override;
  240. virtual void write_newline(int p_line) override;
  241. virtual void write_return(const Address &p_return_value) override;
  242. virtual void write_assert(const Address &p_test, const Address &p_message) override;
  243. virtual ~GDScriptByteCodeGenerator();
  244. };
  245. #endif // GDSCRIPT_BYTE_CODEGEN