Skip to content

Commit

Permalink
ecma_type_t_improve
Browse files Browse the repository at this point in the history
the string handling is too complicated and can not be handled properly
  • Loading branch information
lygstate committed Dec 11, 2024
1 parent 8d44eed commit c64e3c1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 78 deletions.
105 changes: 52 additions & 53 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,52 @@ typedef enum
#endif /* JERRY_VM_THROW */
} ecma_status_flag_t;

/**
* Shift for directly encoded integer in ecma_value_t
*/
#define ECMA_INTEGER_SHIFT 1

/**
* Shift for pointer in ecma_value_t
*/
#define ECMA_POINTER_SHIFT 3

/**
* Shift for encoded values in ecma_value_t
*/
#define ECMA_ENCODED_SHIFT 6

/**
* Type of ecma value
*/
typedef enum
{
ECMA_TYPE_DIRECT = 0, /**< directly encoded value, a 28 bit signed integer or a simple value */
ECMA_TYPE_STRING = 1, /**< pointer to description of a string */
ECMA_TYPE_FLOAT = 2, /**< pointer to a 64 or 32 bit floating point number */
ECMA_TYPE_OBJECT = 3, /**< pointer to description of an object */
ECMA_TYPE_SYMBOL = 4, /**< pointer to description of a symbol */
ECMA_TYPE_DIRECT_STRING = 5, /**< directly encoded string values */
ECMA_TYPE_BIGINT = 6, /**< pointer to a bigint primitive */
ECMA_TYPE_ERROR = 7, /**< pointer to description of an error reference (only supported by C API) */
ECMA_TYPE_SNAPSHOT_OFFSET = ECMA_TYPE_ERROR, /**< offset to a snapshot number/string */
ECMA_TYPE___MAX = ECMA_TYPE_ERROR /** highest value for ecma types */
/**< directly encoded types or encoded thin pointer
* When the total memory size (the base address needs to be provided) is less
* than 512MB ((1 << (32 - 3)) bytes), then the alignment of encoded thin pointer
* is 8 for save memory; otherwise the alignment of encoded thin pointer is 64 for
* quick access
*/
ECMA_TYPE_ENCODED = (0 << ECMA_INTEGER_SHIFT) | 0,

/**< directly encoded string(length <= 3) */
ECMA_TYPE_DIRECT_STRING = (0 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED,
/**< directly encoded magic string(length > 3) id(lit_magic_string_id_t or lit_magic_string_ex_id_t) */
ECMA_TYPE_DIRECT_MAGIC_STRING = (0 << ECMA_ENCODED_SHIFT) | (1 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED,
/**< directly encoded simple value */
ECMA_TYPE_DIRECT_SIMPLE = (1 << ECMA_ENCODED_SHIFT) | (1 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED,
ECMA_TYPE_SYMBOL = (2 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED, /**< encoded thin pointer to symbol value */
/**< encoded thin pointer to description of an error reference (only supported by C API) */
ECMA_TYPE_ERROR = (3 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED,
ECMA_TYPE_BIGINT = (4 << ECMA_POINTER_SHIFT) | ECMA_TYPE_ENCODED, /**< encoded thin pointer to bigint primitive */

ECMA_TYPE_FLOAT = (1 << ECMA_INTEGER_SHIFT) | 0, /**< pointer to a 64 or 32 bit floating point number */
ECMA_TYPE_STRING =
(2 << ECMA_INTEGER_SHIFT) | 0, /**< pointer to description of a string(length > 3) and not in magic string table */
ECMA_TYPE_OBJECT = (3 << ECMA_INTEGER_SHIFT) | 0, /**< pointer to description of an object */
ECMA_TYPE_INTEGER = 1, /**< directly encoded 31 bit signed integer */
ECMA_TYPE_SNAPSHOT_OFFSET = ECMA_TYPE_OBJECT, /**< offset to a snapshot number/string */
ECMA_TYPE___MAX = ECMA_TYPE_OBJECT /** highest value for ecma types */
} ecma_type_t;

/**
Expand Down Expand Up @@ -146,39 +177,19 @@ typedef uint32_t ecma_value_t;
typedef int32_t ecma_integer_value_t;

/**
* Mask for ecma types in ecma_value_t
*/
#define ECMA_VALUE_TYPE_MASK 0x7u

/**
* Shift for value part in ecma_value_t
* Shift for directly encoded values in ecma_value_t
*/
#define ECMA_VALUE_SHIFT 3
#define ECMA_DIRECT_ENCODED_SHIFT 7

/**
* Mask for directly encoded values
*/
#define ECMA_DIRECT_TYPE_MASK ((1u << ECMA_VALUE_SHIFT) | ECMA_VALUE_TYPE_MASK)

/**
* Ecma integer value type
*/
#define ECMA_DIRECT_TYPE_INTEGER_VALUE ((0u << ECMA_VALUE_SHIFT) | ECMA_TYPE_DIRECT)

/**
* Ecma simple value type
*/
#define ECMA_DIRECT_TYPE_SIMPLE_VALUE ((1u << ECMA_VALUE_SHIFT) | ECMA_TYPE_DIRECT)

/**
* Shift for directly encoded values in ecma_value_t
*/
#define ECMA_DIRECT_SHIFT 4
#define ECMA_DIRECT_ENCODED_MASK ((1u << ECMA_DIRECT_ENCODED_SHIFT) - 1)

/**
* ECMA make simple value
*/
#define ECMA_MAKE_VALUE(value) ((((ecma_value_t) (value)) << ECMA_DIRECT_SHIFT) | ECMA_DIRECT_TYPE_SIMPLE_VALUE)
#define ECMA_MAKE_VALUE(value) ((((ecma_value_t) (value)) << ECMA_DIRECT_ENCODED_SHIFT) | ECMA_TYPE_DIRECT_SIMPLE)

/**
* Simple ecma values
Expand Down Expand Up @@ -595,7 +606,7 @@ typedef struct
* Checks whether a property is raw data property (should only be used in assertions)
*/
#define ECMA_PROPERTY_IS_RAW_DATA(property) \
(((property) &ECMA_PROPERTY_FLAG_DATA) && (property) < ECMA_PROPERTY_INTERNAL)
(((property) & ECMA_PROPERTY_FLAG_DATA) && (property) < ECMA_PROPERTY_INTERNAL)

/**
* Create internal property.
Expand Down Expand Up @@ -1367,7 +1378,7 @@ typedef float ecma_number_t;
/**
* Value '-1' of ecma_number_t
*/
#define ECMA_NUMBER_MINUS_ONE ((ecma_number_t) -1.0f)
#define ECMA_NUMBER_MINUS_ONE ((ecma_number_t) - 1.0f)

/**
* Maximum number of characters in string representation of ecma-number
Expand Down Expand Up @@ -1440,11 +1451,7 @@ typedef enum
* Maximum value of the immediate part of a direct magic string.
* Must be compatible with the immediate property name.
*/
#if JERRY_CPOINTER_32_BIT
#define ECMA_DIRECT_STRING_MAX_IMM 0x07ffffff
#else /* !JERRY_CPOINTER_32_BIT */
#define ECMA_DIRECT_STRING_MAX_IMM 0x0000ffff
#endif /* JERRY_CPOINTER_32_BIT */
#define ECMA_DIRECT_STRING_MAX_IMM 999

/**
* Shift for direct string value part in ecma_value_t.
Expand All @@ -1454,7 +1461,7 @@ typedef enum
/**
* Full mask for direct strings.
*/
#define ECMA_DIRECT_STRING_MASK ((uintptr_t) (ECMA_DIRECT_TYPE_MASK | (0x3u << ECMA_VALUE_SHIFT)))
#define ECMA_DIRECT_STRING_MASK ((uintptr_t) (ECMA_ENCODED_TYPE_MASK | (0x3u << ECMA_VALUE_SHIFT)))

/**
* Create an ecma direct string.
Expand Down Expand Up @@ -1513,13 +1520,10 @@ typedef enum
* maximum size is 2^16. */
ECMA_STRING_CONTAINER_LONG_OR_EXTERNAL_STRING, /**< the string is a long string or provided externally
* and only its attributes are stored. */
ECMA_STRING_CONTAINER_UINT32_IN_DESC, /**< string representation of an uint32 number */
ECMA_STRING_CONTAINER_HEAP_ASCII_STRING, /**< actual data is on the heap as an ASCII string
* maximum size is 2^16. */
ECMA_STRING_CONTAINER_MAGIC_STRING_EX, /**< the ecma-string is equal to one of external magic strings */
ECMA_STRING_CONTAINER_SYMBOL, /**< the ecma-string is a symbol */

ECMA_STRING_CONTAINER__MAX = ECMA_STRING_CONTAINER_SYMBOL /**< maximum value */
ECMA_STRING_CONTAINER__MAX = ECMA_STRING_CONTAINER_HEAP_ASCII_STRING /**< maximum value */
} ecma_string_container_t;

/**
Expand Down Expand Up @@ -1580,12 +1584,7 @@ typedef struct
/**
* Actual data or identifier of it's place in container (depending on 'container' field)
*/
union
{
lit_string_hash_t hash; /**< hash of the ASCII/UTF8 string */
uint32_t magic_string_ex_id; /**< identifier of an external magic string (lit_magic_string_ex_id_t) */
uint32_t uint32_number; /**< uint32-represented number placed locally in the descriptor */
} u;
lit_string_hash_t hash; /**< hash of the ASCII/UTF8 string */
} ecma_string_t;

/**
Expand Down Expand Up @@ -1633,7 +1632,7 @@ typedef struct
* Set the size of an ecma ASCII string
*/
#define ECMA_ASCII_STRING_SET_SIZE(string_p, size) \
(*((lit_utf8_byte_t *) (string_p) + sizeof (ecma_string_t)) = (uint8_t) ((size) -1))
(*((lit_utf8_byte_t *) (string_p) + sizeof (ecma_string_t)) = (uint8_t) ((size) - 1))

/**
* Get the start position of the string buffer of an ecma ASCII string
Expand Down
8 changes: 0 additions & 8 deletions jerry-core/ecma/base/ecma-helpers-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,6 @@ ecma_string_get_chars_fast (const ecma_string_t *string_p, /**< ecma-string */
*size_p = ECMA_ASCII_STRING_GET_SIZE (string_p);
return ECMA_ASCII_STRING_GET_BUFFER (string_p);
}
default:
{
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX);

lit_magic_string_ex_id_t id = LIT_MAGIC_STRING__COUNT - string_p->u.magic_string_ex_id;
*size_p = lit_get_magic_string_ex_size (id);
return lit_get_magic_string_ex_utf8 (id);
}
}
} /* ecma_string_get_chars_fast */

Expand Down
10 changes: 5 additions & 5 deletions jerry-core/ecma/base/ecma-helpers-value.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ ecma_is_value_direct (ecma_value_t value) /**< ecma value */
extern inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_is_value_simple (ecma_value_t value) /**< ecma value */
{
return (value & ECMA_DIRECT_TYPE_MASK) == ECMA_DIRECT_TYPE_SIMPLE_VALUE;
return (value & ECMA_ENCODED_TYPE_MASK) == ECMA_TYPE_DIRECT_SIMPLE;
} /* ecma_is_value_simple */

/**
Expand Down Expand Up @@ -261,7 +261,7 @@ ecma_is_value_array_hole (ecma_value_t value) /**< ecma value */
extern inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_is_value_integer_number (ecma_value_t value) /**< ecma value */
{
return (value & ECMA_DIRECT_TYPE_MASK) == ECMA_DIRECT_TYPE_INTEGER_VALUE;
return (value & ECMA_ENCODED_TYPE_MASK) == ECMA_TYPE_INTEGER;
} /* ecma_is_value_integer_number */

/**
Expand All @@ -274,9 +274,9 @@ extern inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_are_values_integer_numbers (ecma_value_t first_value, /**< first ecma value */
ecma_value_t second_value) /**< second ecma value */
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_INTEGER_VALUE == 0, ecma_direct_type_integer_value_must_be_zero);
JERRY_STATIC_ASSERT (ECMA_TYPE_INTEGER == 0, ecma_direct_type_integer_value_must_be_zero);

return ((first_value | second_value) & ECMA_DIRECT_TYPE_MASK) == ECMA_DIRECT_TYPE_INTEGER_VALUE;
return ((first_value | second_value) & ECMA_ENCODED_TYPE_MASK) == ECMA_TYPE_INTEGER;
} /* ecma_are_values_integer_numbers */

/**
Expand Down Expand Up @@ -494,7 +494,7 @@ ecma_make_integer_value (ecma_integer_value_t integer_value) /**< integer number
{
JERRY_ASSERT (ECMA_IS_INTEGER_NUMBER (integer_value));

return (((ecma_value_t) integer_value) << ECMA_DIRECT_SHIFT) | ECMA_DIRECT_TYPE_INTEGER_VALUE;
return (((ecma_value_t) integer_value) << ECMA_INTEGER_SHIFT) | ECMA_TYPE_INTEGER;
} /* ecma_make_integer_value */

/**
Expand Down
25 changes: 13 additions & 12 deletions jerry-core/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ vm_op_get_value (ecma_value_t object, /**< base object */
if (ecma_is_value_integer_number (property))
{
ecma_integer_value_t int_value = ecma_get_integer_from_value (property);

if (int_value >= 0 && int_value <= ECMA_DIRECT_STRING_MAX_IMM)
if (int_value >= 0)
{
if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_ARRAY)
{
Expand All @@ -86,8 +85,10 @@ vm_op_get_value (ecma_value_t object, /**< base object */
}
}
}

property_name_p = (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) int_value);
if (int_value <= ECMA_DIRECT_STRING_MAX_IMM)
{
property_name_p = (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) int_value);
}
}
}
else if (ecma_is_value_string (property))
Expand Down Expand Up @@ -3370,12 +3371,12 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_BIT_NOT:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_is_value_integer_number (left_value))
{
*stack_top_p++ = (~ECMA_DIRECT_TYPE_MASK) ^ left_value;
*stack_top_p++ = (~ECMA_ENCODED_TYPE_MASK) ^ left_value;
goto free_left_value;
}

Expand Down Expand Up @@ -3679,7 +3680,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_BIT_OR:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand All @@ -3700,7 +3701,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_BIT_XOR:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand All @@ -3721,7 +3722,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_BIT_AND:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand All @@ -3742,7 +3743,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_LEFT_SHIFT:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand All @@ -3766,7 +3767,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_RIGHT_SHIFT:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand All @@ -3789,7 +3790,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
case VM_OC_UNS_RIGHT_SHIFT:
{
JERRY_STATIC_ASSERT (ECMA_DIRECT_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
JERRY_STATIC_ASSERT (ECMA_ENCODED_TYPE_MASK == ((1 << ECMA_DIRECT_SHIFT) - 1),
direct_type_mask_must_fill_all_bits_before_the_value_starts);

if (ecma_are_values_integer_numbers (left_value, right_value))
Expand Down

0 comments on commit c64e3c1

Please sign in to comment.