diff --git a/core/safe_refcount.cpp b/core/safe_refcount.cpp deleted file mode 100644 index c330a983a77..00000000000 --- a/core/safe_refcount.cpp +++ /dev/null @@ -1,287 +0,0 @@ -/*************************************************************************/ -/* safe_refcount.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "safe_refcount.h" - -// Atomic functions, these are used for multithread safe reference counters! - -#ifdef NO_THREADS - -/* Bogus implementation unaware of multiprocessing */ - -template -static _ALWAYS_INLINE_ T _atomic_conditional_increment_impl(register T *pw) { - - if (*pw == 0) - return 0; - - (*pw)++; - - return *pw; -} - -template -static _ALWAYS_INLINE_ T _atomic_decrement_impl(register T *pw) { - - (*pw)--; - - return *pw; -} - -template -static _ALWAYS_INLINE_ T _atomic_increment_impl(register T *pw) { - - (*pw)++; - - return *pw; -} - -template -static _ALWAYS_INLINE_ T _atomic_sub_impl(register T *pw, register T val) { - - (*pw) -= val; - - return *pw; -} - -template -static _ALWAYS_INLINE_ T _atomic_add_impl(register T *pw, register T val) { - - (*pw) += val; - - return *pw; -} - -template -static _ALWAYS_INLINE_ T _atomic_exchange_if_greater_impl(register T *pw, register T val) { - - if (val > *pw) - *pw = val; - - return *pw; -} - -#elif defined(__GNUC__) - -/* Implementation for GCC & Clang */ - -// GCC guarantees atomic intrinsics for sizes of 1, 2, 4 and 8 bytes. -// Clang states it supports GCC atomic builtins. - -template -static _ALWAYS_INLINE_ T _atomic_conditional_increment_impl(register T *pw) { - - while (true) { - T tmp = static_cast(*pw); - if (tmp == 0) - return 0; // if zero, can't add to it anymore - if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp) - return tmp + 1; - } -} - -template -static _ALWAYS_INLINE_ T _atomic_decrement_impl(register T *pw) { - - return __sync_sub_and_fetch(pw, 1); -} - -template -static _ALWAYS_INLINE_ T _atomic_increment_impl(register T *pw) { - - return __sync_add_and_fetch(pw, 1); -} - -template -static _ALWAYS_INLINE_ T _atomic_sub_impl(register T *pw, register T val) { - - return __sync_sub_and_fetch(pw, val); -} - -template -static _ALWAYS_INLINE_ T _atomic_add_impl(register T *pw, register T val) { - - return __sync_add_and_fetch(pw, val); -} - -template -static _ALWAYS_INLINE_ T _atomic_exchange_if_greater_impl(register T *pw, register T val) { - - while (true) { - T tmp = static_cast(*pw); - if (tmp >= val) - return tmp; // already greater, or equal - if (__sync_val_compare_and_swap(pw, tmp, val) == tmp) - return val; - } -} - -#elif defined(_MSC_VER) - -/* Implementation for MSVC-Windows */ - -// don't pollute my namespace! -#include - -#define ATOMIC_CONDITIONAL_INCREMENT_BODY(m_pw, m_win_type, m_win_cmpxchg, m_cpp_type) \ - /* try to increment until it actually works */ \ - /* taken from boost */ \ - while (true) { \ - m_cpp_type tmp = static_cast(*(m_pw)); \ - if (tmp == 0) \ - return 0; /* if zero, can't add to it anymore */ \ - if (m_win_cmpxchg((m_win_type volatile *)(m_pw), tmp + 1, tmp) == tmp) \ - return tmp + 1; \ - } - -#define ATOMIC_EXCHANGE_IF_GREATER_BODY(m_pw, m_val, m_win_type, m_win_cmpxchg, m_cpp_type) \ - while (true) { \ - m_cpp_type tmp = static_cast(*(m_pw)); \ - if (tmp >= m_val) \ - return tmp; /* already greater, or equal */ \ - if (m_win_cmpxchg((m_win_type volatile *)(m_pw), m_val, tmp) == tmp) \ - return m_val; \ - } - -static _ALWAYS_INLINE_ uint32_t _atomic_conditional_increment_impl(register uint32_t *pw) { - - ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONG, InterlockedCompareExchange, uint32_t) -} - -static _ALWAYS_INLINE_ uint32_t _atomic_decrement_impl(register uint32_t *pw) { - - return InterlockedDecrement((LONG volatile *)pw); -} - -static _ALWAYS_INLINE_ uint32_t _atomic_increment_impl(register uint32_t *pw) { - - return InterlockedIncrement((LONG volatile *)pw); -} - -static _ALWAYS_INLINE_ uint32_t _atomic_sub_impl(register uint32_t *pw, register uint32_t val) { - - return InterlockedExchangeAdd((LONG volatile *)pw, -(int32_t)val) - val; -} - -static _ALWAYS_INLINE_ uint32_t _atomic_add_impl(register uint32_t *pw, register uint32_t val) { - - return InterlockedAdd((LONG volatile *)pw, val); -} - -static _ALWAYS_INLINE_ uint32_t _atomic_exchange_if_greater_impl(register uint32_t *pw, register uint32_t val) { - - ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONG, InterlockedCompareExchange, uint32_t) -} - -static _ALWAYS_INLINE_ uint64_t _atomic_conditional_increment_impl(register uint64_t *pw) { - - ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONGLONG, InterlockedCompareExchange64, uint64_t) -} - -static _ALWAYS_INLINE_ uint64_t _atomic_decrement_impl(register uint64_t *pw) { - - return InterlockedDecrement64((LONGLONG volatile *)pw); -} - -static _ALWAYS_INLINE_ uint64_t _atomic_increment_impl(register uint64_t *pw) { - - return InterlockedIncrement64((LONGLONG volatile *)pw); -} - -static _ALWAYS_INLINE_ uint64_t _atomic_sub_impl(register uint64_t *pw, register uint64_t val) { - - return InterlockedExchangeAdd64((LONGLONG volatile *)pw, -(int64_t)val) - val; -} - -static _ALWAYS_INLINE_ uint64_t _atomic_add_impl(register uint64_t *pw, register uint64_t val) { - - return InterlockedAdd64((LONGLONG volatile *)pw, val); -} - -static _ALWAYS_INLINE_ uint64_t _atomic_exchange_if_greater_impl(register uint64_t *pw, register uint64_t val) { - - ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONGLONG, InterlockedCompareExchange64, uint64_t) -} - -#else - -//no threads supported? -#error Must provide atomic functions for this platform or compiler! - -#endif - -// The actual advertised functions; they'll call the right implementation - -uint32_t atomic_conditional_increment(register uint32_t *counter) { - return _atomic_conditional_increment_impl(counter); -} - -uint32_t atomic_decrement(register uint32_t *pw) { - return _atomic_decrement_impl(pw); -} - -uint32_t atomic_increment(register uint32_t *pw) { - return _atomic_increment_impl(pw); -} - -uint32_t atomic_sub(register uint32_t *pw, register uint32_t val) { - return _atomic_sub_impl(pw, val); -} - -uint32_t atomic_add(register uint32_t *pw, register uint32_t val) { - return _atomic_add_impl(pw, val); -} - -uint32_t atomic_exchange_if_greater(register uint32_t *pw, register uint32_t val) { - return _atomic_exchange_if_greater_impl(pw, val); -} - -uint64_t atomic_conditional_increment(register uint64_t *counter) { - return _atomic_conditional_increment_impl(counter); -} - -uint64_t atomic_decrement(register uint64_t *pw) { - return _atomic_decrement_impl(pw); -} - -uint64_t atomic_increment(register uint64_t *pw) { - return _atomic_increment_impl(pw); -} - -uint64_t atomic_sub(register uint64_t *pw, register uint64_t val) { - return _atomic_sub_impl(pw, val); -} - -uint64_t atomic_add(register uint64_t *pw, register uint64_t val) { - return _atomic_add_impl(pw, val); -} - -uint64_t atomic_exchange_if_greater(register uint64_t *pw, register uint64_t val) { - return _atomic_exchange_if_greater_impl(pw, val); -} diff --git a/core/safe_refcount.h b/core/safe_refcount.h index 802d84cccc1..05126cc3196 100644 --- a/core/safe_refcount.h +++ b/core/safe_refcount.h @@ -36,19 +36,212 @@ #include "platform_config.h" #include "typedefs.h" -uint32_t atomic_conditional_increment(register uint32_t *counter); -uint32_t atomic_decrement(register uint32_t *pw); -uint32_t atomic_increment(register uint32_t *pw); -uint32_t atomic_sub(register uint32_t *pw, register uint32_t val); -uint32_t atomic_add(register uint32_t *pw, register uint32_t val); -uint32_t atomic_exchange_if_greater(register uint32_t *pw, register uint32_t val); +// Atomic functions, these are used for multithread safe reference counters! -uint64_t atomic_conditional_increment(register uint64_t *counter); -uint64_t atomic_decrement(register uint64_t *pw); -uint64_t atomic_increment(register uint64_t *pw); -uint64_t atomic_sub(register uint64_t *pw, register uint64_t val); -uint64_t atomic_add(register uint64_t *pw, register uint64_t val); -uint64_t atomic_exchange_if_greater(register uint64_t *pw, register uint64_t val); +#ifdef NO_THREADS + +/* Bogus implementation unaware of multiprocessing */ + +template +static _ALWAYS_INLINE_ T atomic_conditional_increment(register T *pw) { + + if (*pw == 0) + return 0; + + (*pw)++; + + return *pw; +} + +template +static _ALWAYS_INLINE_ T atomic_decrement(register T *pw) { + + (*pw)--; + + return *pw; +} + +template +static _ALWAYS_INLINE_ T atomic_increment(register T *pw) { + + (*pw)++; + + return *pw; +} + +template +static _ALWAYS_INLINE_ T atomic_sub(register T *pw, register V val) { + + (*pw) -= val; + + return *pw; +} + +template +static _ALWAYS_INLINE_ T atomic_add(register T *pw, register V val) { + + (*pw) += val; + + return *pw; +} + +template +static _ALWAYS_INLINE_ T atomic_exchange_if_greater(register T *pw, register V val) { + + if (val > *pw) + *pw = val; + + return *pw; +} + +#elif defined(__GNUC__) + +/* Implementation for GCC & Clang */ + +// GCC guarantees atomic intrinsics for sizes of 1, 2, 4 and 8 bytes. +// Clang states it supports GCC atomic builtins. + +template +static _ALWAYS_INLINE_ T atomic_conditional_increment(register T *pw) { + + while (true) { + T tmp = static_cast(*pw); + if (tmp == 0) + return 0; // if zero, can't add to it anymore + if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp) + return tmp + 1; + } +} + +template +static _ALWAYS_INLINE_ T atomic_decrement(register T *pw) { + + return __sync_sub_and_fetch(pw, 1); +} + +template +static _ALWAYS_INLINE_ T atomic_increment(register T *pw) { + + return __sync_add_and_fetch(pw, 1); +} + +template +static _ALWAYS_INLINE_ T atomic_sub(register T *pw, register V val) { + + return __sync_sub_and_fetch(pw, val); +} + +template +static _ALWAYS_INLINE_ T atomic_add(register T *pw, register V val) { + + return __sync_add_and_fetch(pw, val); +} + +template +static _ALWAYS_INLINE_ T atomic_exchange_if_greater(register T *pw, register V val) { + + while (true) { + T tmp = static_cast(*pw); + if (tmp >= val) + return tmp; // already greater, or equal + if (__sync_val_compare_and_swap(pw, tmp, val) == tmp) + return val; + } +} + +#elif defined(_MSC_VER) + +/* Implementation for MSVC-Windows */ + +// don't pollute my namespace! +#include + +#define ATOMIC_CONDITIONAL_INCREMENT_BODY(m_pw, m_win_type, m_win_cmpxchg, m_cpp_type) \ + /* try to increment until it actually works */ \ + /* taken from boost */ \ + while (true) { \ + m_cpp_type tmp = static_cast(*(m_pw)); \ + if (tmp == 0) \ + return 0; /* if zero, can't add to it anymore */ \ + if (m_win_cmpxchg((m_win_type volatile *)(m_pw), tmp + 1, tmp) == tmp) \ + return tmp + 1; \ + } + +#define ATOMIC_EXCHANGE_IF_GREATER_BODY(m_pw, m_val, m_win_type, m_win_cmpxchg, m_cpp_type) \ + while (true) { \ + m_cpp_type tmp = static_cast(*(m_pw)); \ + if (tmp >= m_val) \ + return tmp; /* already greater, or equal */ \ + if (m_win_cmpxchg((m_win_type volatile *)(m_pw), m_val, tmp) == tmp) \ + return m_val; \ + } + +static _ALWAYS_INLINE_ uint32_t atomic_conditional_increment(register uint32_t *pw) { + + ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONG, InterlockedCompareExchange, uint32_t) +} + +static _ALWAYS_INLINE_ uint32_t atomic_decrement(register uint32_t *pw) { + + return InterlockedDecrement((LONG volatile *)pw); +} + +static _ALWAYS_INLINE_ uint32_t atomic_increment(register uint32_t *pw) { + + return InterlockedIncrement((LONG volatile *)pw); +} + +static _ALWAYS_INLINE_ uint32_t atomic_sub(register uint32_t *pw, register uint32_t val) { + + return InterlockedExchangeAdd((LONG volatile *)pw, -(int32_t)val) - val; +} + +static _ALWAYS_INLINE_ uint32_t atomic_add(register uint32_t *pw, register uint32_t val) { + + return InterlockedAdd((LONG volatile *)pw, val); +} + +static _ALWAYS_INLINE_ uint32_t atomic_exchange_if_greater(register uint32_t *pw, register uint32_t val) { + + ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONG, InterlockedCompareExchange, uint32_t) +} + +static _ALWAYS_INLINE_ uint64_t atomic_conditional_increment(register uint64_t *pw) { + + ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONGLONG, InterlockedCompareExchange64, uint64_t) +} + +static _ALWAYS_INLINE_ uint64_t atomic_decrement(register uint64_t *pw) { + + return InterlockedDecrement64((LONGLONG volatile *)pw); +} + +static _ALWAYS_INLINE_ uint64_t atomic_increment(register uint64_t *pw) { + + return InterlockedIncrement64((LONGLONG volatile *)pw); +} + +static _ALWAYS_INLINE_ uint64_t atomic_sub(register uint64_t *pw, register uint64_t val) { + + return InterlockedExchangeAdd64((LONGLONG volatile *)pw, -(int64_t)val) - val; +} + +static _ALWAYS_INLINE_ uint64_t atomic_add(register uint64_t *pw, register uint64_t val) { + + return InterlockedAdd64((LONGLONG volatile *)pw, val); +} + +static _ALWAYS_INLINE_ uint64_t atomic_exchange_if_greater(register uint64_t *pw, register uint64_t val) { + + ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONGLONG, InterlockedCompareExchange64, uint64_t) +} + +#else + +//no threads supported? +#error Must provide atomic functions for this platform or compiler! + +#endif struct SafeRefCount { @@ -57,17 +250,17 @@ struct SafeRefCount { public: // destroy() is called when weak_count_ drops to zero. - bool ref() { //true on success + _ALWAYS_INLINE_ bool ref() { //true on success return atomic_conditional_increment(&count) != 0; } - uint32_t refval() { //true on success + _ALWAYS_INLINE_ uint32_t refval() { //true on success return atomic_conditional_increment(&count); } - bool unref() { // true if must be disposed of + _ALWAYS_INLINE_ bool unref() { // true if must be disposed of if (atomic_decrement(&count) == 0) { return true; @@ -76,12 +269,12 @@ public: return false; } - uint32_t get() const { // nothrow + _ALWAYS_INLINE_ uint32_t get() const { // nothrow return count; } - void init(uint32_t p_value = 1) { + _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { count = p_value; } diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 4065b6a8445..03ec3362911 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -100,32 +100,32 @@ } /* clang-format on */ -#define CASES(PREFIX) static void *switch_table_##PREFIX[25][27] = { \ - TYPES(PREFIX, OP_EQUAL), \ - TYPES(PREFIX, OP_NOT_EQUAL), \ - TYPES(PREFIX, OP_LESS), \ - TYPES(PREFIX, OP_LESS_EQUAL), \ - TYPES(PREFIX, OP_GREATER), \ - TYPES(PREFIX, OP_GREATER_EQUAL), \ - TYPES(PREFIX, OP_ADD), \ - TYPES(PREFIX, OP_SUBTRACT), \ - TYPES(PREFIX, OP_MULTIPLY), \ - TYPES(PREFIX, OP_DIVIDE), \ - TYPES(PREFIX, OP_NEGATE), \ - TYPES(PREFIX, OP_POSITIVE), \ - TYPES(PREFIX, OP_MODULE), \ - TYPES(PREFIX, OP_STRING_CONCAT), \ - TYPES(PREFIX, OP_SHIFT_LEFT), \ - TYPES(PREFIX, OP_SHIFT_RIGHT), \ - TYPES(PREFIX, OP_BIT_AND), \ - TYPES(PREFIX, OP_BIT_OR), \ - TYPES(PREFIX, OP_BIT_XOR), \ - TYPES(PREFIX, OP_BIT_NEGATE), \ - TYPES(PREFIX, OP_AND), \ - TYPES(PREFIX, OP_OR), \ - TYPES(PREFIX, OP_XOR), \ - TYPES(PREFIX, OP_NOT), \ - TYPES(PREFIX, OP_IN), \ +#define CASES(PREFIX) static const void *switch_table_##PREFIX[25][27] = { \ + TYPES(PREFIX, OP_EQUAL), \ + TYPES(PREFIX, OP_NOT_EQUAL), \ + TYPES(PREFIX, OP_LESS), \ + TYPES(PREFIX, OP_LESS_EQUAL), \ + TYPES(PREFIX, OP_GREATER), \ + TYPES(PREFIX, OP_GREATER_EQUAL), \ + TYPES(PREFIX, OP_ADD), \ + TYPES(PREFIX, OP_SUBTRACT), \ + TYPES(PREFIX, OP_MULTIPLY), \ + TYPES(PREFIX, OP_DIVIDE), \ + TYPES(PREFIX, OP_NEGATE), \ + TYPES(PREFIX, OP_POSITIVE), \ + TYPES(PREFIX, OP_MODULE), \ + TYPES(PREFIX, OP_STRING_CONCAT), \ + TYPES(PREFIX, OP_SHIFT_LEFT), \ + TYPES(PREFIX, OP_SHIFT_RIGHT), \ + TYPES(PREFIX, OP_BIT_AND), \ + TYPES(PREFIX, OP_BIT_OR), \ + TYPES(PREFIX, OP_BIT_XOR), \ + TYPES(PREFIX, OP_BIT_NEGATE), \ + TYPES(PREFIX, OP_AND), \ + TYPES(PREFIX, OP_OR), \ + TYPES(PREFIX, OP_XOR), \ + TYPES(PREFIX, OP_NOT), \ + TYPES(PREFIX, OP_IN), \ } #define SWITCH(PREFIX, op, val) goto *switch_table_##PREFIX[op][val]; diff --git a/modules/gdscript/gd_function.cpp b/modules/gdscript/gd_function.cpp index 70340f0823a..d39e967ac9d 100644 --- a/modules/gdscript/gd_function.cpp +++ b/modules/gdscript/gd_function.cpp @@ -41,11 +41,12 @@ Variant *GDFunction::_get_variant(int p_address, GDInstance *p_instance, GDScrip switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) { case ADDR_TYPE_SELF: { - +#ifdef DEBUG_ENABLED if (unlikely(!p_instance)) { r_error = "Cannot access self without instance."; return NULL; } +#endif return &self; } break; case ADDR_TYPE_CLASS: { @@ -53,18 +54,22 @@ Variant *GDFunction::_get_variant(int p_address, GDInstance *p_instance, GDScrip return &p_script->_static_ref; } break; case ADDR_TYPE_MEMBER: { - //member indexing is O(1) +#ifdef DEBUG_ENABLED if (unlikely(!p_instance)) { r_error = "Cannot access member without instance."; return NULL; } +#endif + //member indexing is O(1) return &p_instance->members[address]; } break; case ADDR_TYPE_CLASS_CONSTANT: { //todo change to index! GDScript *o = p_script; +#ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _global_names_count, NULL); +#endif const StringName *sn = &_global_names_ptr[address]; while (o) { @@ -84,18 +89,22 @@ Variant *GDFunction::_get_variant(int p_address, GDInstance *p_instance, GDScrip ERR_FAIL_V(NULL); } break; case ADDR_TYPE_LOCAL_CONSTANT: { +#ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _constant_count, NULL); +#endif return &_constants_ptr[address]; } break; case ADDR_TYPE_STACK: case ADDR_TYPE_STACK_VARIABLE: { +#ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _stack_size, NULL); +#endif return &p_stack[address]; } break; case ADDR_TYPE_GLOBAL: { - +#ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL); - +#endif return &GDScriptLanguage::get_singleton()->get_global_array()[address]; } break; case ADDR_TYPE_NIL: { @@ -161,8 +170,71 @@ static String _get_var_type(const Variant *p_type) { return basestr; } +#if defined(__GNUC__) && !defined(__clang__) +#define OPCODES_TABLE \ + static const void *switch_table_ops[] = { \ + &&OPCODE_OPERATOR, \ + &&OPCODE_EXTENDS_TEST, \ + &&OPCODE_SET, \ + &&OPCODE_GET, \ + &&OPCODE_SET_NAMED, \ + &&OPCODE_GET_NAMED, \ + &&OPCODE_SET_MEMBER, \ + &&OPCODE_GET_MEMBER, \ + &&OPCODE_ASSIGN, \ + &&OPCODE_ASSIGN_TRUE, \ + &&OPCODE_ASSIGN_FALSE, \ + &&OPCODE_CONSTRUCT, \ + &&OPCODE_CONSTRUCT_ARRAY, \ + &&OPCODE_CONSTRUCT_DICTIONARY, \ + &&OPCODE_CALL, \ + &&OPCODE_CALL_RETURN, \ + &&OPCODE_CALL_BUILT_IN, \ + &&OPCODE_CALL_SELF, \ + &&OPCODE_CALL_SELF_BASE, \ + &&OPCODE_YIELD, \ + &&OPCODE_YIELD_SIGNAL, \ + &&OPCODE_YIELD_RESUME, \ + &&OPCODE_JUMP, \ + &&OPCODE_JUMP_IF, \ + &&OPCODE_JUMP_IF_NOT, \ + &&OPCODE_JUMP_TO_DEF_ARGUMENT, \ + &&OPCODE_RETURN, \ + &&OPCODE_ITERATE_BEGIN, \ + &&OPCODE_ITERATE, \ + &&OPCODE_ASSERT, \ + &&OPCODE_BREAKPOINT, \ + &&OPCODE_LINE, \ + &&OPCODE_END \ + }; + +#define OPCODE(m_op) \ + m_op: +#define OPCODE_WHILE(m_test) +#define OPCODES_END \ + OPSEXIT: +#define OPCODES_OUT \ + OPSOUT: +#define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]] +#define OPCODE_SWITCH(m_test) DISPATCH_OPCODE; +#define OPCODE_BREAK goto OPSEXIT +#define OPCODE_OUT goto OPSOUT +#else +#define OPCODES_TABLE +#define OPCODE(m_op) case m_op: +#define OPCODE_WHILE(m_test) while (m_test) +#define OPCODES_END +#define OPCODES_OUT +#define DISPATCH_OPCODE continue +#define OPCODE_SWITCH(m_test) switch (m_test) +#define OPCODE_BREAK break +#define OPCODE_OUT break +#endif + Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) { + OPCODES_TABLE; + if (!_code_ptr) { return Variant(); @@ -271,16 +343,23 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (ScriptDebugger::get_singleton()) GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line); -#define GD_ERR_BREAK(m_cond) ERR_BREAK(m_cond) +#define GD_ERR_BREAK(m_cond) \ + { \ + if (unlikely(m_cond)) { \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \ + OPCODE_BREAK; \ + } else \ + _err_error_exists = false; \ + } #define CHECK_SPACE(m_space) \ - ERR_BREAK((ip + m_space) > _code_size) + GD_ERR_BREAK((ip + m_space) > _code_size) #define GET_VARIANT_PTR(m_v, m_code_ofs) \ Variant *m_v; \ m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text); \ if (unlikely(!m_v)) \ - break; + OPCODE_BREAK; #else #define GD_ERR_BREAK(m_cond) @@ -306,15 +385,15 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a bool exit_ok = false; #ifdef DEBUG_ENABLED - while (ip < _code_size) { + OPCODE_WHILE(ip < _code_size) { int last_opcode = _code_ptr[ip]; #else - while (true) { + OPCODE_WHILE(true) { #endif - switch (_code_ptr[ip]) { + OPCODE_SWITCH(_code_ptr[ip]) { - case OPCODE_OPERATOR: { + OPCODE(OPCODE_OPERATOR) { CHECK_SPACE(5); @@ -343,14 +422,14 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a } else { err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'."; } - break; + OPCODE_BREAK; } *dst = ret; #endif ip += 5; - continue; + DISPATCH_OPCODE; } - case OPCODE_EXTENDS_TEST: { + OPCODE(OPCODE_EXTENDS_TEST) { CHECK_SPACE(4); @@ -362,12 +441,12 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) { err_text = "Left operand of 'is' is not an instance of anything."; - break; + OPCODE_BREAK; } if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) { err_text = "Right operand of 'is' is not a class."; - break; + OPCODE_BREAK; } #endif Object *obj_A = *a; @@ -390,7 +469,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (cmp == scr_B) { //inherits from script, all ok extends_ok = true; - break; + OPCODE_BREAK; } cmp = cmp->_base; @@ -405,7 +484,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (!nc) { err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "')."; - break; + OPCODE_BREAK; } #endif extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name()); @@ -413,9 +492,9 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = extends_ok; ip += 4; - continue; + DISPATCH_OPCODE; } - case OPCODE_SET: { + OPCODE(OPCODE_SET) { CHECK_SPACE(3); @@ -435,13 +514,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a v = "of type '" + _get_var_type(index) + "'"; } err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "')."; - break; + OPCODE_BREAK; } #endif ip += 4; - continue; + DISPATCH_OPCODE; } - case OPCODE_GET: { + OPCODE(OPCODE_GET) { CHECK_SPACE(3); @@ -466,14 +545,14 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a v = "of type '" + _get_var_type(index) + "'"; } err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "')."; - break; + OPCODE_BREAK; } *dst = ret; #endif ip += 4; - continue; + DISPATCH_OPCODE; } - case OPCODE_SET_NAMED: { + OPCODE(OPCODE_SET_NAMED) { CHECK_SPACE(3); @@ -492,13 +571,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (!valid) { String err_type; err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "')."; - break; + OPCODE_BREAK; } #endif ip += 4; - continue; + DISPATCH_OPCODE; } - case OPCODE_GET_NAMED: { + OPCODE(OPCODE_GET_NAMED) { CHECK_SPACE(4); @@ -525,14 +604,14 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a } else { err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "')."; } - break; + OPCODE_BREAK; } *dst = ret; #endif ip += 4; - continue; + DISPATCH_OPCODE; } - case OPCODE_SET_MEMBER: { + OPCODE(OPCODE_SET_MEMBER) { CHECK_SPACE(3); int indexname = _code_ptr[ip + 1]; @@ -545,16 +624,16 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!ok) { err_text = "Internal error setting property: " + String(*index); - break; + OPCODE_BREAK; } else if (!valid) { err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + "."; - break; + OPCODE_BREAK; } #endif ip += 3; - continue; + DISPATCH_OPCODE; } - case OPCODE_GET_MEMBER: { + OPCODE(OPCODE_GET_MEMBER) { CHECK_SPACE(3); int indexname = _code_ptr[ip + 1]; @@ -566,13 +645,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!ok) { err_text = "Internal error getting property: " + String(*index); - break; + OPCODE_BREAK; } #endif ip += 3; - continue; + DISPATCH_OPCODE; } - case OPCODE_ASSIGN: { + OPCODE(OPCODE_ASSIGN) { CHECK_SPACE(3); GET_VARIANT_PTR(dst, 1); @@ -581,9 +660,9 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = *src; ip += 3; - continue; + DISPATCH_OPCODE; } - case OPCODE_ASSIGN_TRUE: { + OPCODE(OPCODE_ASSIGN_TRUE) { CHECK_SPACE(2); GET_VARIANT_PTR(dst, 1); @@ -591,9 +670,9 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = true; ip += 2; - continue; + DISPATCH_OPCODE; } - case OPCODE_ASSIGN_FALSE: { + OPCODE(OPCODE_ASSIGN_FALSE) { CHECK_SPACE(2); GET_VARIANT_PTR(dst, 1); @@ -601,9 +680,9 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = false; ip += 2; - continue; + DISPATCH_OPCODE; } - case OPCODE_CONSTRUCT: { + OPCODE(OPCODE_CONSTRUCT) { CHECK_SPACE(2); Variant::Type t = Variant::Type(_code_ptr[ip + 1]); @@ -623,15 +702,15 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (err.error != Variant::CallError::CALL_OK) { err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs); - break; + OPCODE_BREAK; } #endif ip += 4 + argc; //construct a basic type - continue; + DISPATCH_OPCODE; } - case OPCODE_CONSTRUCT_ARRAY: { + OPCODE(OPCODE_CONSTRUCT_ARRAY) { CHECK_SPACE(1); int argc = _code_ptr[ip + 1]; @@ -649,9 +728,9 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = array; ip += 3 + argc; - continue; + DISPATCH_OPCODE; } - case OPCODE_CONSTRUCT_DICTIONARY: { + OPCODE(OPCODE_CONSTRUCT_DICTIONARY) { CHECK_SPACE(1); int argc = _code_ptr[ip + 1]; @@ -671,10 +750,10 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a *dst = dict; ip += 3 + argc * 2; - continue; + DISPATCH_OPCODE; } - case OPCODE_CALL_RETURN: - case OPCODE_CALL: { + OPCODE(OPCODE_CALL_RETURN) + OPCODE(OPCODE_CALL) { CHECK_SPACE(4); bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN; @@ -736,24 +815,24 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (base->is_ref()) { err_text = "Attempted to free a reference."; - break; + OPCODE_BREAK; } else if (base->get_type() == Variant::OBJECT) { err_text = "Attempted to free a locked object (calling or emitting)."; - break; + OPCODE_BREAK; } } } err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs); - break; + OPCODE_BREAK; } #endif //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack); ip += argc + 1; - continue; + DISPATCH_OPCODE; } - case OPCODE_CALL_BUILT_IN: { + OPCODE(OPCODE_CALL_BUILT_IN) { CHECK_SPACE(4); @@ -786,17 +865,17 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a } else { err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs); } - break; + OPCODE_BREAK; } #endif ip += argc + 1; - continue; + DISPATCH_OPCODE; } - case OPCODE_CALL_SELF: { + OPCODE(OPCODE_CALL_SELF) { - break; + OPCODE_BREAK; } - case OPCODE_CALL_SELF_BASE: { + OPCODE(OPCODE_CALL_SELF_BASE) { CHECK_SPACE(2); int self_fun = _code_ptr[ip + 1]; @@ -805,7 +884,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (self_fun < 0 || self_fun >= _global_names_count) { err_text = "compiler bug, function name not found"; - break; + OPCODE_BREAK; } #endif const StringName *methodname = &_global_names_ptr[self_fun]; @@ -830,7 +909,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a gds = gds->base.ptr(); E = gds->member_functions.find(*methodname); if (E) - break; + OPCODE_BREAK; } Variant::CallError err; @@ -865,14 +944,14 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a String methodstr = *methodname; err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs); - break; + OPCODE_BREAK; } ip += 4 + argc; - continue; + DISPATCH_OPCODE; } - case OPCODE_YIELD: - case OPCODE_YIELD_SIGNAL: { + OPCODE(OPCODE_YIELD) + OPCODE(OPCODE_YIELD_SIGNAL) { int ipofs = 1; if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) { @@ -913,11 +992,11 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (argobj->get_type() != Variant::OBJECT) { err_text = "First argument of yield() not of type object."; - break; + OPCODE_BREAK; } if (argname->get_type() != Variant::STRING) { err_text = "Second argument of yield() not a string (for signal name)."; - break; + OPCODE_BREAK; } #endif Object *obj = argobj->operator Object *(); @@ -926,18 +1005,18 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (!obj) { err_text = "First argument of yield() is null."; - break; + OPCODE_BREAK; } if (ScriptDebugger::get_singleton()) { if (!ObjectDB::instance_validate(obj)) { err_text = "First argument of yield() is a previously freed instance."; - break; + OPCODE_BREAK; } } if (signal.length() == 0) { err_text = "Second argument of yield() is an empty string (for signal name)."; - break; + OPCODE_BREAK; } #endif @@ -945,38 +1024,38 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (err != OK) { err_text = "Error connecting to signal: " + signal + " during yield()."; - break; + OPCODE_BREAK; } #endif } exit_ok = true; - break; + OPCODE_BREAK; } - case OPCODE_YIELD_RESUME: { + OPCODE(OPCODE_YIELD_RESUME) { CHECK_SPACE(2); #ifdef DEBUG_ENABLED if (!p_state) { err_text = ("Invalid Resume (bug?)"); - break; + OPCODE_BREAK; } #endif GET_VARIANT_PTR(result, 1); *result = p_state->result; ip += 2; - continue; + DISPATCH_OPCODE; } - case OPCODE_JUMP: { + OPCODE(OPCODE_JUMP) { CHECK_SPACE(2); int to = _code_ptr[ip + 1]; GD_ERR_BREAK(to < 0 || to > _code_size); ip = to; - continue; + DISPATCH_OPCODE; } - case OPCODE_JUMP_IF: { + OPCODE(OPCODE_JUMP_IF) { CHECK_SPACE(3); @@ -988,12 +1067,12 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a int to = _code_ptr[ip + 2]; GD_ERR_BREAK(to < 0 || to > _code_size); ip = to; - continue; + DISPATCH_OPCODE; } ip += 3; - continue; + DISPATCH_OPCODE; } - case OPCODE_JUMP_IF_NOT: { + OPCODE(OPCODE_JUMP_IF_NOT) { CHECK_SPACE(3); @@ -1005,26 +1084,26 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a int to = _code_ptr[ip + 2]; GD_ERR_BREAK(to < 0 || to > _code_size); ip = to; - continue; + DISPATCH_OPCODE; } ip += 3; - continue; + DISPATCH_OPCODE; } - case OPCODE_JUMP_TO_DEF_ARGUMENT: { + OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) { CHECK_SPACE(2); ip = _default_arg_ptr[defarg]; - continue; + DISPATCH_OPCODE; } - case OPCODE_RETURN: { + OPCODE(OPCODE_RETURN) { CHECK_SPACE(2); GET_VARIANT_PTR(r, 1); retvalue = *r; exit_ok = true; - break; + OPCODE_BREAK; } - case OPCODE_ITERATE_BEGIN: { + OPCODE(OPCODE_ITERATE_BEGIN) { CHECK_SPACE(8); //space for this a regular iterate @@ -1036,13 +1115,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!valid) { err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'."; - break; + OPCODE_BREAK; } #endif int jumpto = _code_ptr[ip + 3]; GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; - continue; + DISPATCH_OPCODE; } GET_VARIANT_PTR(iterator, 4); @@ -1050,13 +1129,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!valid) { err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'."; - break; + OPCODE_BREAK; } #endif ip += 5; //skip regular iterate which is always next - continue; + DISPATCH_OPCODE; } - case OPCODE_ITERATE: { + OPCODE(OPCODE_ITERATE) { CHECK_SPACE(4); @@ -1068,13 +1147,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!valid) { err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?)."; - break; + OPCODE_BREAK; } #endif int jumpto = _code_ptr[ip + 3]; GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; - continue; + DISPATCH_OPCODE; } GET_VARIANT_PTR(iterator, 4); @@ -1082,13 +1161,13 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a #ifdef DEBUG_ENABLED if (!valid) { err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?)."; - break; + OPCODE_BREAK; } #endif ip += 5; //loop again - continue; + DISPATCH_OPCODE; } - case OPCODE_ASSERT: { + OPCODE(OPCODE_ASSERT) { CHECK_SPACE(2); GET_VARIANT_PTR(test, 1); @@ -1098,23 +1177,23 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (!result) { err_text = "Assertion failed."; - break; + OPCODE_BREAK; } #endif ip += 2; - continue; + DISPATCH_OPCODE; } - case OPCODE_BREAKPOINT: { + OPCODE(OPCODE_BREAKPOINT) { #ifdef DEBUG_ENABLED if (ScriptDebugger::get_singleton()) { GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true); } #endif ip += 1; - continue; + DISPATCH_OPCODE; } - case OPCODE_LINE: { + OPCODE(OPCODE_LINE) { CHECK_SPACE(2); line = _code_ptr[ip + 1]; @@ -1141,23 +1220,26 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a ScriptDebugger::get_singleton()->line_poll(); } - continue; + DISPATCH_OPCODE; } - case OPCODE_END: { + OPCODE(OPCODE_END) { exit_ok = true; - break; + OPCODE_BREAK; } +#if 0 default: { err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip); - break; + OPCODE_BREAK; } +#endif } + OPCODES_END #ifdef DEBUG_ENABLED if (exit_ok) - break; + OPCODE_OUT; //error // function, file, line, error, explanation String err_file; @@ -1182,9 +1264,10 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a } #endif - break; + OPCODE_OUT; } + OPCODES_OUT #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling) { uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;