Skip to content

Commit 50d124b

Browse files
sand1kruben-ayrapetyan
authored andcommitted
Parser optimizations.
- parser is now non-recursive (i.e. parse function is not called recursively in any case); - byte-code is now more compact: - constants are now not immediately dumped upon occurence, but later - where necessary; - assignments are combined with unary / binary operations; - binary operations are encoded more compactly in many cases; - byte-code arrays are now allocated separately for each scope (so, GC of the scopes now becomes possible); - byte-code is dumped directly into corresponding byte-code arrays: - linked lists of op_meta are not now used for main code of a scope. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov a.shitov@samsung.com
1 parent b1de93a commit 50d124b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8598
-6955
lines changed

jerry-core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ project (JerryCore CXX C ASM)
112112
${CMAKE_SOURCE_DIR}/jerry-core/ecma/base
113113
${CMAKE_SOURCE_DIR}/jerry-core/ecma/operations
114114
${CMAKE_SOURCE_DIR}/jerry-core/parser/js
115+
${CMAKE_SOURCE_DIR}/jerry-core/parser/js/bc
115116
${CMAKE_SOURCE_DIR}/jerry-core/parser/js/collections
116117
${CMAKE_SOURCE_DIR}/jerry-core/parser/regexp
117118
${CMAKE_SOURCE_DIR}/jerry-core/jrt)
@@ -131,6 +132,7 @@ project (JerryCore CXX C ASM)
131132
file(GLOB SOURCE_CORE_ECMA_BASE ecma/base/*.cpp)
132133
file(GLOB SOURCE_CORE_ECMA_OPERATIONS ecma/operations/*.cpp)
133134
file(GLOB SOURCE_CORE_PARSER_JS parser/js/*.cpp)
135+
file(GLOB SOURCE_CORE_PARSER_JS_BC parser/js/bc/*.cpp)
134136
file(GLOB SOURCE_CORE_PARSER_JS_COLLECTIONS parser/js/collections/*.cpp)
135137
file(GLOB SOURCE_CORE_PARSER_REGEXP parser/regexp/*.cpp)
136138
file(GLOB SOURCE_CORE_JRT jrt/*.cpp)
@@ -145,6 +147,7 @@ project (JerryCore CXX C ASM)
145147
${SOURCE_CORE_ECMA_BASE}
146148
${SOURCE_CORE_ECMA_OPERATIONS}
147149
${SOURCE_CORE_PARSER_JS}
150+
${SOURCE_CORE_PARSER_JS_BC}
148151
${SOURCE_CORE_PARSER_JS_COLLECTIONS}
149152
${SOURCE_CORE_PARSER_REGEXP}
150153
${SOURCE_CORE_JRT})

jerry-core/ecma/base/ecma-globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ FIXME (Move to library that should define the type (literal.h /* ? */))
795795
typedef rcs_record_t *literal_t;
796796
typedef rcs_cpointer_t lit_cpointer_t;
797797

798+
#define NOT_A_LITERAL (lit_cpointer_t::null_cp ())
799+
798800
/**
799801
* ECMA string-value descriptor
800802
*/

jerry-core/ecma/base/ecma-helpers-string.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @{
2121
*/
2222

23+
#include "bytecode-data.h"
2324
#include "ecma-alloc.h"
2425
#include "ecma-gc.h"
2526
#include "ecma-globals.h"
@@ -29,7 +30,6 @@
2930
#include "jrt-libc-includes.h"
3031
#include "lit-char-helpers.h"
3132
#include "lit-magic-strings.h"
32-
#include "serializer.h"
3333
#include "vm.h"
3434

3535
/**

jerry-core/ecma/builtin-objects/ecma-builtin-function.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "ecma-function-object.h"
2222
#include "ecma-lex-env.h"
2323
#include "ecma-try-catch-macro.h"
24-
#include "serializer.h"
2524
#include "lit-magic-strings.h"
2625
#include "parser.h"
2726

jerry-core/ecma/operations/ecma-eval.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "bytecode-data.h"
1617
#include "ecma-builtins.h"
1718
#include "ecma-exceptions.h"
1819
#include "ecma-eval.h"
@@ -21,7 +22,6 @@
2122
#include "ecma-helpers.h"
2223
#include "ecma-lex-env.h"
2324
#include "parser.h"
24-
#include "serializer.h"
2525
#include "vm.h"
2626

2727
/** \addtogroup ecma ECMA
@@ -114,7 +114,7 @@ ecma_op_eval_chars_buffer (const jerry_api_char_t *code_p, /**< code characters
114114

115115
if (!code_contains_functions)
116116
{
117-
serializer_remove_bytecode_data (bytecode_data_p);
117+
bc_remove_bytecode_data (bytecode_data_p);
118118
}
119119
}
120120

jerry-core/ecma/operations/ecma-function-object.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "bytecode-data.h"
1617
#include "ecma-alloc.h"
1718
#include "ecma-builtin-helpers.h"
1819
#include "ecma-builtins.h"
@@ -255,28 +256,27 @@ ecma_op_create_function_object (ecma_collection_header_t *formal_params_collecti
255256
bool is_no_lex_env = false;
256257

257258
vm_instr_counter_t instr_pos = first_instr_pos;
258-
opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (bytecode_header_p, instr_pos++);
259259

260-
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT)
260+
if (bytecode_header_p->is_strict)
261261
{
262262
is_strict_mode_code = true;
263263
}
264264

265-
if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER)
266-
&& (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER))
265+
if (!bytecode_header_p->is_ref_arguments_identifier
266+
&& !bytecode_header_p->is_ref_eval_identifier)
267267
{
268268
/* the code doesn't use 'arguments' identifier
269269
* and doesn't perform direct call to eval,
270270
* so Arguments object can't be referenced */
271271
do_instantiate_arguments_object = false;
272272
}
273273

274-
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS)
274+
if (bytecode_header_p->is_args_moved_to_regs)
275275
{
276276
is_arguments_moved_to_regs = true;
277277
}
278278

279-
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV)
279+
if (bytecode_header_p->is_no_lex_env)
280280
{
281281
is_no_lex_env = true;
282282
}

jerry-core/jerry-internal.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ jerry_is_abort_on_fail (void);
3838
typedef struct
3939
{
4040
uint32_t lit_table_size; /**< size of literal table */
41-
uint32_t bytecode_size; /**< size of instructions array */
42-
uint32_t idx_to_lit_map_size; /** size of idx-to-lit map */
41+
uint32_t scopes_num; /**< number of saved bytecode pieces in the snapshot */
4342
uint32_t is_run_global : 1; /**< flag, indicating whether the snapshot
4443
* was dumped as 'Global scope'-mode code (true)
4544
* or as eval-mode code (false) */
@@ -48,6 +47,6 @@ typedef struct
4847
/**
4948
* Jerry snapshot format version
5049
*/
51-
#define JERRY_SNAPSHOT_VERSION (1u)
50+
#define JERRY_SNAPSHOT_VERSION (2u)
5251

5352
#endif /* !JERRY_INTERNAL_H */

jerry-core/jerry.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <stdio.h>
1717

18+
#include "bytecode-data.h"
1819
#include "ecma-alloc.h"
1920
#include "ecma-array-object.h"
2021
#include "ecma-builtins.h"
@@ -27,9 +28,9 @@
2728
#include "ecma-objects.h"
2829
#include "ecma-objects-general.h"
2930
#include "ecma-try-catch-macro.h"
31+
#include "lit-literal.h"
3032
#include "lit-magic-strings.h"
3133
#include "parser.h"
32-
#include "serializer.h"
3334

3435
#define JERRY_INTERNAL
3536
#include "jerry-internal.h"
@@ -1420,7 +1421,7 @@ jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */
14201421
jerry_make_api_available ();
14211422

14221423
mem_init ();
1423-
serializer_init ();
1424+
lit_init ();
14241425
ecma_init ();
14251426
} /* jerry_init */
14261427

@@ -1435,7 +1436,8 @@ jerry_cleanup (void)
14351436
bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0);
14361437

14371438
ecma_finalize ();
1438-
serializer_free ();
1439+
lit_finalize ();
1440+
bc_finalize ();
14391441
mem_finalize (is_show_mem_stats);
14401442
vm_finalize ();
14411443
} /* jerry_cleanup */
@@ -1684,11 +1686,11 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
16841686

16851687
size_t header_offset = buffer_write_offset;
16861688

1687-
if (buffer_write_offset + sizeof (jerry_snapshot_header_t) > buffer_size)
1689+
if (buffer_write_offset + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT) > buffer_size)
16881690
{
16891691
return 0;
16901692
}
1691-
buffer_write_offset += sizeof (jerry_snapshot_header_t);
1693+
buffer_write_offset += JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT);
16921694

16931695
lit_mem_to_snapshot_id_map_entry_t* lit_map_p = NULL;
16941696
uint32_t literals_num;
@@ -1704,17 +1706,21 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
17041706
return 0;
17051707
}
17061708

1707-
size_t bytecode_offset = sizeof (version) + sizeof (jerry_snapshot_header_t) + header.lit_table_size;
1709+
size_t bytecode_offset = (sizeof (version)
1710+
+ JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT)
1711+
+ header.lit_table_size);
1712+
17081713
JERRY_ASSERT (JERRY_ALIGNUP (bytecode_offset, MEM_ALIGNMENT) == bytecode_offset);
17091714

1710-
bool is_ok = serializer_dump_bytecode_with_idx_map (buffer_p,
1711-
buffer_size,
1712-
&buffer_write_offset,
1713-
bytecode_data_p,
1714-
lit_map_p,
1715-
literals_num,
1716-
&header.bytecode_size,
1717-
&header.idx_to_lit_map_size);
1715+
bool is_ok = bc_save_bytecode_data (buffer_p,
1716+
buffer_size,
1717+
&buffer_write_offset,
1718+
bytecode_data_p,
1719+
lit_map_p,
1720+
literals_num,
1721+
&header.scopes_num);
1722+
1723+
JERRY_ASSERT (header.scopes_num != 0);
17181724

17191725
if (lit_map_p != NULL)
17201726
{
@@ -1782,12 +1788,12 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
17821788
}
17831789

17841790
const jerry_snapshot_header_t *header_p = (const jerry_snapshot_header_t *) (snapshot_data_p + snapshot_read);
1785-
if (snapshot_read + sizeof (jerry_snapshot_header_t) > snapshot_size)
1791+
if (snapshot_read + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT) > snapshot_size)
17861792
{
17871793
return JERRY_COMPLETION_CODE_INVALID_SNAPSHOT_FORMAT;
17881794
}
17891795

1790-
snapshot_read += sizeof (jerry_snapshot_header_t);
1796+
snapshot_read += JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT);
17911797

17921798
if (snapshot_read + header_p->lit_table_size > snapshot_size)
17931799
{
@@ -1809,19 +1815,19 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
18091815

18101816
snapshot_read += header_p->lit_table_size;
18111817

1812-
if (snapshot_read + header_p->bytecode_size + header_p->idx_to_lit_map_size > snapshot_size)
1818+
if (snapshot_read > snapshot_size)
18131819
{
18141820
mem_heap_free_block (lit_map_p);
18151821
return JERRY_COMPLETION_CODE_INVALID_SNAPSHOT_FORMAT;
18161822
}
18171823

18181824
const bytecode_data_header_t *bytecode_data_p;
1819-
bytecode_data_p = serializer_load_bytecode_with_idx_map (snapshot_data_p + snapshot_read,
1820-
header_p->bytecode_size,
1821-
header_p->idx_to_lit_map_size,
1822-
lit_map_p,
1823-
literals_num,
1824-
is_copy);
1825+
bytecode_data_p = bc_load_bytecode_data (snapshot_data_p + snapshot_read,
1826+
snapshot_size - snapshot_read,
1827+
lit_map_p,
1828+
literals_num,
1829+
is_copy,
1830+
header_p->scopes_num);
18251831

18261832
if (lit_map_p != NULL)
18271833
{

jerry-core/lit/lit-literal-storage.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include "bytecode-data.h"
1617
#include "ecma-helpers.h"
1718
#include "jrt.h"
1819
#include "lit-literal-storage.h"
@@ -666,26 +667,16 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer *
666667
}
667668
}
668669

669-
uint32_t aligned_size = JERRY_ALIGNUP (lit_table_size, MEM_ALIGNMENT);
670-
671-
if (aligned_size != lit_table_size)
670+
if (!bc_align_data_in_output_buffer (&lit_table_size,
671+
buffer_p,
672+
buffer_size,
673+
in_out_buffer_offset_p))
672674
{
673-
JERRY_ASSERT (aligned_size > lit_table_size);
674-
675-
uint8_t padding = 0;
676-
uint32_t padding_bytes_num = (uint32_t) (aligned_size - lit_table_size);
677-
678-
for (uint32_t i = 0; i < padding_bytes_num; i++)
679-
{
680-
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, padding))
681-
{
682-
return false;
683-
}
684-
}
675+
return false;
685676
}
686677

687678
*out_map_num_p = literals_num;
688-
*out_lit_table_size_p = aligned_size;
679+
*out_lit_table_size_p = lit_table_size;
689680

690681
return true;
691682
} /* lit_dump_literals_for_snapshot */

0 commit comments

Comments
 (0)