Skip to content

Literal storage #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jerry-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ project (JerryCore CXX C ASM)
# Include directories
set(INCLUDE_CORE
${CMAKE_SOURCE_DIR}/jerry-core
${CMAKE_SOURCE_DIR}/jerry-core/lit
${CMAKE_SOURCE_DIR}/jerry-core/rcs
${CMAKE_SOURCE_DIR}/jerry-core/mem
${CMAKE_SOURCE_DIR}/jerry-core/vm
Expand All @@ -110,6 +111,7 @@ project (JerryCore CXX C ASM)
# Sources
# Jerry core
file(GLOB SOURCE_CORE_API *.cpp)
file(GLOB SOURCE_CORE_LIT lit/*.cpp)
file(GLOB SOURCE_CORE_RCS rcs/*.cpp)
file(GLOB SOURCE_CORE_MEM mem/*.cpp)
file(GLOB SOURCE_CORE_VM vm/*.cpp)
Expand All @@ -123,6 +125,7 @@ project (JerryCore CXX C ASM)
set(SOURCE_CORE
jerry.cpp
${SOURCE_CORE_API}
${SOURCE_CORE_LIT}
${SOURCE_CORE_RCS}
${SOURCE_CORE_MEM}
${SOURCE_CORE_VM}
Expand Down
9 changes: 6 additions & 3 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "config.h"
#include "jrt.h"
#include "mem-allocator.h"
#include "rcs-recordset.h"

/** \addtogroup compressedpointer Compressed pointer
* @{
Expand Down Expand Up @@ -757,10 +758,12 @@ typedef enum
} ecma_string_container_t;

FIXME (Move to library that should define the type (literal.h /* ? */))

/**
* Index in literal table
* Literal and compressed pointer to literal
*/
typedef uint32_t literal_index_t;
typedef rcs_record_t *literal_t;
typedef rcs_cpointer_t lit_cpointer_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to lit-literal.h?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruben-ayrapetyan I suppose we should, but ecma-globals.h has dependencies on lit_cpointer_t, while lit_* depends on ecma_globals.h

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@egavrin, I see. In the case, probably we should update the 'FIXME' comment above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruben-ayrapetyan we may leave it as is, since we're going to fix anyway. But with a separate pr.


/**
* Identifiers of ECMA and implementation-defined magic string constants
Expand Down Expand Up @@ -814,7 +817,7 @@ typedef struct ecma_string_t
union
{
/** Index of string in literal table */
literal_index_t lit_index;
lit_cpointer_t lit_cp;

/** Compressed pointer to an ecma_collection_header_t */
mem_cpointer_t collection_cp : ECMA_POINTER_FIELD_WIDTH;
Expand Down
158 changes: 68 additions & 90 deletions jerry-core/ecma/base/ecma-helpers-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ static ecma_length_t ecma_magic_string_max_length;
#endif /* !JERRY_NDEBUG */

static void
ecma_init_ecma_string_from_lit_index (ecma_string_t *string_p,
literal_index_t lit_index,
bool is_stack_var);
ecma_init_ecma_string_from_lit_cp (ecma_string_t *string_p,
lit_cpointer_t lit_index,
bool is_stack_var);
static void
ecma_init_ecma_string_from_magic_string_id (ecma_string_t *string_p,
ecma_magic_string_id_t magic_string_id,
Expand Down Expand Up @@ -370,42 +370,42 @@ ecma_get_magic_string_ex_count (void)
* Initialize ecma-string descriptor with string described by index in literal table
*/
static void
ecma_init_ecma_string_from_lit_index (ecma_string_t *string_p, /**< descriptor to initialize */
literal_index_t lit_index, /**< index in the literal table */
bool is_stack_var) /**< flag indicating whether the string descriptor
ecma_init_ecma_string_from_lit_cp (ecma_string_t *string_p, /**< descriptor to initialize */
lit_cpointer_t lit_cp, /**< compressed pointer to literal */
bool is_stack_var) /**< flag indicating whether the string descriptor
is placed on stack (true) or in the heap (false) */
{
#ifndef JERRY_NDEBUG
JERRY_ASSERT (is_stack_var == (!mem_is_heap_pointer (string_p)));
#endif /* !JERRY_NDEBUG */

const literal lit = serializer_get_literal_by_id (lit_index);
if (lit.type == LIT_MAGIC_STR)
literal_t lit = lit_get_literal_by_cp (lit_cp);
if (lit->get_type () == LIT_MAGIC_STR_T)
{
ecma_init_ecma_string_from_magic_string_id (string_p,
lit.data.magic_str_id,
lit_magic_record_get_magic_str_id (lit),
is_stack_var);

return;
}
else if (lit.type == LIT_MAGIC_STR_EX)
else if (lit->get_type () == LIT_MAGIC_STR_EX_T)
{
ecma_init_ecma_string_from_magic_string_ex_id (string_p,
lit.data.magic_str_ex_id,
lit_magic_record_ex_get_magic_str_id (lit),
is_stack_var);

return;
}
JERRY_ASSERT (lit.type == LIT_STR);

JERRY_ASSERT (lit->get_type () == LIT_STR_T);

string_p->refs = 1;
string_p->is_stack_var = (is_stack_var != 0);
string_p->container = ECMA_STRING_CONTAINER_LIT_TABLE;
string_p->hash = lit.data.lp.hash;
string_p->hash = lit_charset_literal_get_hash (lit);

string_p->u.common_field = 0;
string_p->u.lit_index = lit_index;
} /* ecma_init_ecma_string_from_lit_index */
string_p->u.lit_cp = lit_cp;
} /* ecma_init_ecma_string_from_lit_cp */

/**
* Initialize ecma-string descriptor with specified magic string
Expand Down Expand Up @@ -591,27 +591,27 @@ ecma_new_ecma_string_from_number (ecma_number_t num) /**< ecma-number */
* with string described by index in literal table
*/
void
ecma_new_ecma_string_on_stack_from_lit_index (ecma_string_t *string_p, /**< pointer to the ecma-string
ecma_new_ecma_string_on_stack_from_lit_cp (ecma_string_t *string_p, /**< pointer to the ecma-string
descriptor to initialize */
literal_index_t lit_index) /**< index in the literal table */
lit_cpointer_t lit_cp) /**< compressed pointer to literal */
{
ecma_init_ecma_string_from_lit_index (string_p, lit_index, true);
} /* ecma_new_ecma_string_on_stack_from_lit_index */
ecma_init_ecma_string_from_lit_cp (string_p, lit_cp, true);
} /* ecma_new_ecma_string_on_stack_from_lit_cp */

/**
* Allocate new ecma-string and fill it with reference to string literal
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t*
ecma_new_ecma_string_from_lit_index (literal_index_t lit_index) /**< index in the literal table */
ecma_new_ecma_string_from_lit_cp (lit_cpointer_t lit_cp) /**< index in the literal table */
{
ecma_string_t* string_desc_p = ecma_alloc_string ();

ecma_init_ecma_string_from_lit_index (string_desc_p, lit_index, false);
ecma_init_ecma_string_from_lit_cp (string_desc_p, lit_cp, false);

return string_desc_p;
} /* ecma_new_ecma_string_from_lit_index */
} /* ecma_new_ecma_string_from_lit_cp */

/**
* Initialize ecma-string descriptor placed on stack with specified magic string
Expand Down Expand Up @@ -1037,13 +1037,9 @@ ecma_string_to_zt_string (const ecma_string_t *string_desc_p, /**< ecma-string d
}
case ECMA_STRING_CONTAINER_LIT_TABLE:
{
const literal lit = serializer_get_literal_by_id (string_desc_p->u.lit_index);
JERRY_ASSERT (lit.type == LIT_STR);
const ecma_char_t *str_p = literal_to_zt (lit);
JERRY_ASSERT (str_p != NULL);

ecma_copy_zt_string_to_buffer (str_p, buffer_p, required_buffer_size);

literal_t lit = lit_get_literal_by_cp (string_desc_p->u.lit_cp);
JERRY_ASSERT (lit->get_type () == LIT_STR_T);
lit_literal_to_charset (lit, buffer_p, (size_t) required_buffer_size);
break;
}
case ECMA_STRING_CONTAINER_UINT32_IN_DESC:
Expand Down Expand Up @@ -1141,7 +1137,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
{
if (string1_p->container == ECMA_STRING_CONTAINER_LIT_TABLE)
{
JERRY_ASSERT (string1_p->u.lit_index != string2_p->u.lit_index);
JERRY_ASSERT (string1_p->u.lit_cp.packed_value != string2_p->u.lit_cp.packed_value);

return false;
}
Expand Down Expand Up @@ -1214,7 +1210,7 @@ ecma_compare_ecma_strings_longpath (const ecma_string_t *string1_p, /* ecma-stri
}
case ECMA_STRING_CONTAINER_LIT_TABLE:
{
JERRY_ASSERT (string1_p->u.lit_index != string2_p->u.lit_index);
JERRY_ASSERT (string1_p->u.lit_cp.packed_value != string2_p->u.lit_cp.packed_value);

return false;
}
Expand Down Expand Up @@ -1346,74 +1342,56 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma-
ecma_char_t zt_string1_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER + 1];
ecma_char_t zt_string2_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER + 1];

if (string1_p->container == ECMA_STRING_CONTAINER_LIT_TABLE)
{
const literal lit = serializer_get_literal_by_id (string1_p->u.lit_index);
JERRY_ASSERT (lit.type == LIT_STR);
zt_string1_p = literal_to_zt (lit);
}
else
{
ssize_t req_size = ecma_string_to_zt_string (string1_p,
zt_string1_buffer,
sizeof (zt_string1_buffer));
ssize_t req_size = ecma_string_to_zt_string (string1_p,
zt_string1_buffer,
sizeof (zt_string1_buffer));

if (req_size < 0)
if (req_size < 0)
{
ecma_char_t *heap_buffer_p = (ecma_char_t*) mem_heap_alloc_block ((size_t) -req_size, MEM_HEAP_ALLOC_SHORT_TERM);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why req_size is negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to ecma_string_to_zt_string (see above) returns negative number in case of insufficient buffer size (third argument). This negative number designates required buffer size.

if (heap_buffer_p == NULL)
{
ecma_char_t *heap_buffer_p = (ecma_char_t*) mem_heap_alloc_block ((size_t) -req_size, MEM_HEAP_ALLOC_SHORT_TERM);
if (heap_buffer_p == NULL)
{
jerry_fatal (ERR_OUT_OF_MEMORY);
}
jerry_fatal (ERR_OUT_OF_MEMORY);
}

ssize_t bytes_copied = ecma_string_to_zt_string (string1_p,
heap_buffer_p,
-req_size);
ssize_t bytes_copied = ecma_string_to_zt_string (string1_p,
heap_buffer_p,
-req_size);

JERRY_ASSERT (bytes_copied > 0);
JERRY_ASSERT (bytes_copied > 0);

zt_string1_p = heap_buffer_p;
is_zt_string1_on_heap = true;
}
else
{
zt_string1_p = zt_string1_buffer;
}
}

if (string2_p->container == ECMA_STRING_CONTAINER_LIT_TABLE)
{
const literal lit = serializer_get_literal_by_id (string2_p->u.lit_index);
JERRY_ASSERT (lit.type == LIT_STR);
zt_string2_p = literal_to_zt (lit);
zt_string1_p = heap_buffer_p;
is_zt_string1_on_heap = true;
}
else
{
ssize_t req_size = ecma_string_to_zt_string (string2_p,
zt_string2_buffer,
sizeof (zt_string2_buffer));
zt_string1_p = zt_string1_buffer;
}

if (req_size < 0)
req_size = ecma_string_to_zt_string (string2_p,
zt_string2_buffer,
sizeof (zt_string2_buffer));

if (req_size < 0)
{
ecma_char_t *heap_buffer_p = (ecma_char_t*) mem_heap_alloc_block ((size_t) -req_size, MEM_HEAP_ALLOC_SHORT_TERM);
if (heap_buffer_p == NULL)
{
ecma_char_t *heap_buffer_p = (ecma_char_t*) mem_heap_alloc_block ((size_t) -req_size, MEM_HEAP_ALLOC_SHORT_TERM);
if (heap_buffer_p == NULL)
{
jerry_fatal (ERR_OUT_OF_MEMORY);
}
jerry_fatal (ERR_OUT_OF_MEMORY);
}

ssize_t bytes_copied = ecma_string_to_zt_string (string2_p,
heap_buffer_p,
-req_size);
ssize_t bytes_copied = ecma_string_to_zt_string (string2_p,
heap_buffer_p,
-req_size);

JERRY_ASSERT (bytes_copied > 0);
JERRY_ASSERT (bytes_copied > 0);

zt_string2_p = heap_buffer_p;
is_zt_string2_on_heap = true;
}
else
{
zt_string2_p = zt_string2_buffer;
}
zt_string2_p = heap_buffer_p;
is_zt_string2_on_heap = true;
}
else
{
zt_string2_p = zt_string2_buffer;
}

bool is_first_less_than_second = ecma_compare_zt_strings_relational (zt_string1_p,
Expand Down Expand Up @@ -1445,9 +1423,9 @@ ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */

if (container == ECMA_STRING_CONTAINER_LIT_TABLE)
{
const literal lit = serializer_get_literal_by_id (string_p->u.lit_index);

return lit.data.lp.length;
literal_t lit = lit_get_literal_by_cp (string_p->u.lit_cp);
JERRY_ASSERT (lit->get_type () == LIT_STR_T);
return lit_charset_record_get_length (lit);
}
else if (container == ECMA_STRING_CONTAINER_MAGIC_STRING)
{
Expand Down
6 changes: 3 additions & 3 deletions jerry-core/ecma/base/ecma-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ extern bool ecma_is_completion_value_empty (ecma_completion_value_t value);
extern ecma_string_t* ecma_new_ecma_string (const ecma_char_t *string_p);
extern ecma_string_t* ecma_new_ecma_string_from_uint32 (uint32_t uint_number);
extern ecma_string_t* ecma_new_ecma_string_from_number (ecma_number_t number);
extern void ecma_new_ecma_string_on_stack_from_lit_index (ecma_string_t *string_p,
literal_index_t lit_index);
extern ecma_string_t* ecma_new_ecma_string_from_lit_index (literal_index_t lit_index);
extern void ecma_new_ecma_string_on_stack_from_lit_cp (ecma_string_t *string_p,
lit_cpointer_t lit_index);
extern ecma_string_t *ecma_new_ecma_string_from_lit_cp (lit_cpointer_t lit_cp);
extern void ecma_new_ecma_string_on_stack_from_magic_string_id (ecma_string_t *string_p,
ecma_magic_string_id_t id);
extern ecma_string_t* ecma_new_ecma_string_from_magic_string_id (ecma_magic_string_id_t id);
Expand Down
2 changes: 0 additions & 2 deletions jerry-core/ecma/operations/ecma-function-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-lex-env.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-objects-arguments.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"

#define JERRY_INTERNAL
#include "jerry-internal.h"
Expand Down
5 changes: 0 additions & 5 deletions jerry-core/jerry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@
#include "ecma-init-finalize.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "jerry.h"
#include "jrt.h"
#include "mem-heap.h"
#include "mem-poolman.h"
#include "parser.h"
#include "serializer.h"
#include "vm.h"

#define JERRY_INTERNAL
#include "jerry-internal.h"
Expand Down
12 changes: 12 additions & 0 deletions jerry-core/jrt/jrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,16 @@ extern void __noreturn jerry_fatal (jerry_fatal_code_t code);
#define JERRY_MIN(v1, v2) ((v1 < v2) ? v1 : v2)
#define JERRY_MAX(v1, v2) ((v1 < v2) ? v2 : v1)

/**
* Placement new operator (constructs an object on a pre-allocated buffer)
*
* Our version of the libc library doesn't support calling the constructors and destructors of the static variables.
* It is proposed to use placement new operator. Generally it is available via #include <new>,
* To fix the unavailability of the header in some configurations placement new operator is implemented here.
*/
inline void* operator new (size_t, void* where)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add comment to the code with proper explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

{
return where;
} /* operator new */

#endif /* !JERRY_GLOBALS_H */
Loading