diff --git a/src/libecmaobjects/ecma-compressed-pointers.h b/src/libecmaobjects/ecma-compressed-pointers.h index 8eed5c08c..30f2a6804 100644 --- a/src/libecmaobjects/ecma-compressed-pointers.h +++ b/src/libecmaobjects/ecma-compressed-pointers.h @@ -20,8 +20,31 @@ /** \addtogroup ecma ECMA * @{ + */ + +/** \addtogroup compressedpointer Compressed pointer + * @{ + */ + +/** + * Ecma-pointer field is used to calculate ecma-value's address. * - * \addtogroup ecmacompressedpointers Helpers for operations with compressed heap pointers + * Ecma-pointer contains value's shifted offset from common Ecma-pointers' base. + * The offset is shifted right by MEM_ALIGNMENT_LOG. + * Least significant MEM_ALIGNMENT_LOG bits of non-shifted offset are zeroes. + */ +#define ECMA_POINTER_FIELD_WIDTH MEM_COMPRESSED_POINTER_WIDTH + +/** + * The NULL value for compressed pointers + */ +#define ECMA_NULL_POINTER MEM_COMPRESSED_POINTER_NULL + +/** + * @} + */ + +/** \addtogroup ecmacompressedpointers Helpers for operations with compressed heap pointers * @{ */ diff --git a/src/libecmaobjects/ecma-globals.h b/src/libecmaobjects/ecma-globals.h index 0895035d0..84c4cf27e 100644 --- a/src/libecmaobjects/ecma-globals.h +++ b/src/libecmaobjects/ecma-globals.h @@ -24,31 +24,10 @@ #define JERRY_ECMA_GLOBALS_H #include "config.h" +#include "ecma-managed-pointer.h" #include "globals.h" #include "mem-allocator.h" -/** \addtogroup compressedpointer Compressed pointer - * @{ - */ - -/** - * Ecma-pointer field is used to calculate ecma-value's address. - * - * Ecma-pointer contains value's shifted offset from common Ecma-pointers' base. - * The offset is shifted right by MEM_ALIGNMENT_LOG. - * Least significant MEM_ALIGNMENT_LOG bits of non-shifted offset are zeroes. - */ -#define ECMA_POINTER_FIELD_WIDTH MEM_COMPRESSED_POINTER_WIDTH - -/** - * The NULL value for compressed pointers - */ -#define ECMA_NULL_POINTER MEM_COMPRESSED_POINTER_NULL - -/** - * @} - */ - /** * Type of ecma-value */ @@ -769,6 +748,13 @@ typedef struct } u; } ecma_string_t; +/** + * Managed pointers definition + */ +typedef ecma_pointer_t ecma_number_ptr_t; +typedef ecma_pointer_t ecma_string_ptr_t; +typedef ecma_pointer_t ecma_object_ptr_t; + /** * @} */ diff --git a/src/libecmaobjects/ecma-managed-pointer.h b/src/libecmaobjects/ecma-managed-pointer.h new file mode 100644 index 000000000..b7349c30a --- /dev/null +++ b/src/libecmaobjects/ecma-managed-pointer.h @@ -0,0 +1,133 @@ +/* Copyright 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ecma-compressed-pointers.h" + +#ifndef ECMA_MANAGED_POINTER_H +#define ECMA_MANAGED_POINTER_H + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmamanagedpointers ECMA managed on-stack pointers + * @{ + */ + +/** + * Untyped (generic) managed pointer + */ +class ecma_generic_ptr_t +{ + public: + /* Constructors */ + __attribute_always_inline__ + ecma_generic_ptr_t () : _ptr (NULL) {} + + ecma_generic_ptr_t (const ecma_generic_ptr_t&) = delete; + ecma_generic_ptr_t (ecma_generic_ptr_t&) = delete; + ecma_generic_ptr_t (ecma_generic_ptr_t&&) = delete; + + /* Getter */ + template + operator T () const + { + return static_cast (_ptr); + } + + /* Assignment operators */ + __attribute_always_inline__ + ecma_generic_ptr_t& operator = (void* ptr) /**< new pointer value */ + { + _ptr = ptr; + + return *this; + } + + __attribute_always_inline__ + ecma_generic_ptr_t& operator = (const ecma_generic_ptr_t& ptr) /**< managed pointer + * to take value from */ + { + _ptr = ptr._ptr; + + return *this; + } + + ecma_generic_ptr_t& operator = (ecma_generic_ptr_t &) = delete; + ecma_generic_ptr_t& operator = (ecma_generic_ptr_t &&) = delete; + + /* Packing to compressed pointer */ + __attribute_always_inline__ + void pack_to (uintptr_t& compressed_pointer) const /**< reference to compressed pointer */ + { + ECMA_SET_NON_NULL_POINTER (compressed_pointer, _ptr); + } + + /* Unpacking from compressed pointer */ + __attribute_always_inline__ + void unpack_from (uintptr_t compressed_pointer) /**< compressed pointer */ + { + *this = ECMA_GET_NON_NULL_POINTER (void, compressed_pointer); + } + + protected: /* accessible to ecma_pointer_t */ + void *_ptr; /* pointer storage */ +}; + +/** + * Typed interface to ecma_generic_ptr_t + */ +template +class ecma_pointer_t : protected ecma_generic_ptr_t +{ + public: + /* Member access */ + __attribute_always_inline__ + T* operator -> () const + { + return _ptr; + } + + /* Dereference */ + __attribute_always_inline__ + T operator * () const + { + return *_ptr; + } + + /* Assignment operators */ + __attribute_always_inline__ + ecma_generic_ptr_t& operator = (T* ptr) /**< new pointer value */ + { + _ptr = ptr; + + return *this; + } + + __attribute_always_inline__ + ecma_pointer_t& operator = (const T& value) /**< value to assign to variable + * under the pointer */ + { + *_ptr = value; + + return *this; + } +}; + +/** + * @} + * @} + */ + +#endif /* !ECMA_MANAGED_POINTER_H */ diff --git a/src/libecmaobjects/ecma-value.h b/src/libecmaobjects/ecma-value.h index 321c28e58..b8a971edd 100644 --- a/src/libecmaobjects/ecma-value.h +++ b/src/libecmaobjects/ecma-value.h @@ -15,6 +15,7 @@ #include "ecma-compressed-pointers.h" #include "ecma-globals.h" +#include "ecma-managed-pointer.h" #include "jrt-bit-fields.h" #ifndef ECMA_VALUE_H @@ -112,7 +113,7 @@ class ecma_value_t || _type == ECMA_TYPE_STRING || _type == ECMA_TYPE_OBJECT); - ECMA_SET_NON_NULL_POINTER (value, _value_p); + _value_p.pack_to (value); } return pack (_type, value); @@ -178,7 +179,7 @@ class ecma_value_t || _type == ECMA_TYPE_STRING || _type == ECMA_TYPE_OBJECT); - _value_p = ECMA_GET_NON_NULL_POINTER (void, value); + _value_p.unpack_from (value); } return *this; @@ -324,7 +325,7 @@ class ecma_value_t union { ecma_simple_value_t _simple_value; - void* _value_p; + ecma_generic_ptr_t _value_p; }; };