diff --git a/jerry-core/ecma/base/ecma-helpers-string.cpp b/jerry-core/ecma/base/ecma-helpers-string.cpp index ce1189487..5db16a637 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.cpp +++ b/jerry-core/ecma/base/ecma-helpers-string.cpp @@ -20,7 +20,6 @@ * @{ */ -#include "deserializer.h" #include "ecma-alloc.h" #include "ecma-gc.h" #include "ecma-globals.h" @@ -28,6 +27,7 @@ #include "ecma-lcache.h" #include "jrt.h" #include "jrt-libc-includes.h" +#include "serializer.h" #include "vm.h" /** @@ -309,7 +309,7 @@ ecma_init_ecma_string_from_lit_index (ecma_string_t *string_p, /**< descriptor t JERRY_ASSERT (is_stack_var == (!mem_is_heap_pointer (string_p))); #endif /* !JERRY_NDEBUG */ - const literal lit = deserialize_literal_by_id (lit_index); + const literal lit = serializer_get_literal_by_id (lit_index); if (lit.type == LIT_MAGIC_STR) { ecma_init_ecma_string_from_magic_string_id (string_p, @@ -903,7 +903,7 @@ ecma_string_to_zt_string (const ecma_string_t *string_desc_p, /**< ecma-string d } case ECMA_STRING_CONTAINER_LIT_TABLE: { - const literal lit = deserialize_literal_by_id (string_desc_p->u.lit_index); + 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); @@ -1189,7 +1189,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma- if (string1_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) { - const literal lit = deserialize_literal_by_id (string1_p->u.lit_index); + 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); } @@ -1224,7 +1224,7 @@ ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, /**< ecma- if (string2_p->container == ECMA_STRING_CONTAINER_LIT_TABLE) { - const literal lit = deserialize_literal_by_id (string2_p->u.lit_index); + 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); } @@ -1286,7 +1286,7 @@ ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */ if (container == ECMA_STRING_CONTAINER_LIT_TABLE) { - const literal lit = deserialize_literal_by_id (string_p->u.lit_index); + const literal lit = serializer_get_literal_by_id (string_p->u.lit_index); return lit.data.lp.length; } diff --git a/jerry-core/jerry.cpp b/jerry-core/jerry.cpp index 34e2f9656..39f6bc85f 100644 --- a/jerry-core/jerry.cpp +++ b/jerry-core/jerry.cpp @@ -15,7 +15,6 @@ #include -#include "deserializer.h" #include "ecma-alloc.h" #include "ecma-builtins.h" #include "ecma-extension.h" @@ -762,7 +761,7 @@ jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */ #endif /* !MEM_STATS */ mem_init (); - deserializer_init (); + serializer_init (); ecma_init (); } /* jerry_init */ @@ -779,7 +778,7 @@ jerry_cleanup (void) bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0); ecma_finalize (); - deserializer_free (); + serializer_free (); mem_finalize (is_show_mem_stats); } /* jerry_cleanup */ @@ -838,7 +837,7 @@ jerry_parse (jerry_ctx_t* ctx_p, /**< run context */ parser_init (source_p, source_size, is_show_opcodes); parser_parse_program (); - const opcode_t* opcodes = (const opcode_t*) deserialize_bytecode (); + const opcode_t* opcodes = (const opcode_t*) serializer_get_bytecode (); serializer_print_opcodes (); parser_free (); diff --git a/jerry-core/parser/collections/lit-id-hash-table.cpp b/jerry-core/parser/collections/lit-id-hash-table.cpp index a0becf9ec..c61aa4e5a 100644 --- a/jerry-core/parser/collections/lit-id-hash-table.cpp +++ b/jerry-core/parser/collections/lit-id-hash-table.cpp @@ -14,9 +14,9 @@ */ #include "lit-id-hash-table.h" +#include "bytecode-data.h" #include "mem-heap.h" #include "jrt-libc-includes.h" -#include "bytecode-data.h" lit_id_hash_table * lit_id_hash_table_init (size_t buckets_count, size_t blocks_count) diff --git a/jerry-core/parser/collections/literal.h b/jerry-core/parser/collections/literal.h deleted file mode 100644 index f8ace2beb..000000000 --- a/jerry-core/parser/collections/literal.h +++ /dev/null @@ -1,62 +0,0 @@ -/* 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. - */ - -#ifndef LITERAL_H -#define LITERAL_H - -#include "ecma-globals.h" -#include "lp-string.h" - -typedef enum __attr_packed___ -{ - LIT_UNKNOWN, - LIT_STR, - LIT_MAGIC_STR, - LIT_NUMBER -} -literal_type; - -typedef struct -{ - union - { - ecma_magic_string_id_t magic_str_id; - ecma_number_t num; - lp_string lp; - void *none; - } - data; - literal_type type; -} -literal; - -#define LITERAL_TO_REWRITE (INVALID_VALUE - 1) - -literal create_empty_literal (void); -literal create_literal_from_num (ecma_number_t); -literal create_literal_from_str (const char *, ecma_length_t); -literal create_literal_from_str_compute_len (const char *); -literal create_literal_from_zt (const ecma_char_t *, ecma_length_t); -bool literal_equal (literal, literal); -bool literal_equal_s (literal, const char *); -bool literal_equal_zt (literal, const ecma_char_t *); -bool literal_equal_num (literal, ecma_number_t); -bool literal_equal_type (literal, literal); -bool literal_equal_type_s (literal, const char *); -bool literal_equal_type_zt (literal, const ecma_char_t *); -bool literal_equal_type_num (literal, ecma_number_t); -const ecma_char_t *literal_to_zt (literal); - -#endif /* LITERAL_H */ diff --git a/jerry-core/parser/collections/tree.h b/jerry-core/parser/collections/tree.h deleted file mode 100644 index 4261ddffc..000000000 --- a/jerry-core/parser/collections/tree.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright 2014 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. - */ - -#ifndef TREE_H -#define TREE_H - -#include "linked-list.h" - -typedef struct tree_header -{ - struct tree_header *parent; - linked_list children; - uint8_t children_num; -} -tree_header; - -#endif /* TREE_H */ diff --git a/jerry-core/parser/js/bytecode-data.h b/jerry-core/parser/js/bytecode-data.h index 777644cf1..978d226e2 100644 --- a/jerry-core/parser/js/bytecode-data.h +++ b/jerry-core/parser/js/bytecode-data.h @@ -17,24 +17,27 @@ #define BYTECODE_DATA_H #include "opcodes.h" -#include "stack.h" -#include "jrt-libc-includes.h" #include "literal.h" -#include "scopes-tree.h" #include "lit-id-hash-table.h" +/* + * All literals are kept in the 'literals' array. + * Literal structure doesn't hold real string. All program-specific strings + * are kept in the 'strings_buffer' and literal has pointer to this buffer. + * + * Literal id is its index in 'literals' array of bytecode_data_t structure. + * + * Bytecode, which is kept in the 'opcodes' field, is divided into blocks + * of 'BLOCK_SIZE' operands. Every block has its own numbering of literals. + * Literal uid could be in range [0, 127] in every block. + * + * To map uid to literal id 'lit_id_hash' table is used. + */ #define BLOCK_SIZE 64 typedef struct { - opcode_counter_t oc; - idx_t uid; - uint8_t reserved; -} -lit_id_table_key; - -typedef struct -{ + const ecma_char_t *strings_buffer; const literal *literals; const opcode_t *opcodes; lit_id_hash_table *lit_id_hash; @@ -42,7 +45,4 @@ typedef struct opcode_counter_t opcodes_count; } bytecode_data_t; -extern bytecode_data_t bytecode_data; -extern scopes_tree current_scope; - #endif // BYTECODE_DATA_H diff --git a/jerry-core/parser/js/deserializer.cpp b/jerry-core/parser/js/deserializer.cpp deleted file mode 100644 index eb1e3cb76..000000000 --- a/jerry-core/parser/js/deserializer.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright 2014 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 "deserializer.h" -#include "bytecode-data.h" -#include "ecma-helpers.h" - -const ecma_char_t *strings_buffer; - -void -deserializer_set_strings_buffer (const ecma_char_t *s) -{ - strings_buffer = s; -} - -literal -deserialize_literal_by_id (literal_index_t id) -{ - JERRY_ASSERT (id != INVALID_LITERAL); - JERRY_ASSERT (id < bytecode_data.literals_count); - return bytecode_data.literals[id]; -} - -literal_index_t -deserialize_lit_id_by_uid (uint8_t id, opcode_counter_t oc) -{ - if (bytecode_data.lit_id_hash == null_hash) - { - return INVALID_LITERAL; - } - return lit_id_hash_table_lookup (bytecode_data.lit_id_hash, id, oc); -} - -const void * -deserialize_bytecode (void) -{ - JERRY_ASSERT (bytecode_data.opcodes != NULL); - return bytecode_data.opcodes; -} - -opcode_t -deserialize_opcode (opcode_counter_t oc) -{ - if (bytecode_data.opcodes == NULL) - { - return deserialize_op_meta (oc).op; - } - JERRY_ASSERT (oc < bytecode_data.opcodes_count); - return bytecode_data.opcodes[oc]; -} - -op_meta -deserialize_op_meta (opcode_counter_t oc) -{ - JERRY_ASSERT (current_scope); - return scopes_tree_op_meta (current_scope, oc); -} - -uint8_t -deserialize_min_temp (void) -{ - return 128; -} - -void -deserializer_init (void) -{ - bytecode_data.literals = NULL; - strings_buffer = NULL; - bytecode_data.opcodes = NULL; -} - -void -deserializer_free (void) -{ - if (strings_buffer) - { - mem_heap_free_block ((uint8_t *) strings_buffer); - } - if (bytecode_data.lit_id_hash != NULL) - { - lit_id_hash_table_free (bytecode_data.lit_id_hash); - } - if (bytecode_data.literals != NULL) - { - mem_heap_free_block ((uint8_t *) bytecode_data.literals); - } - mem_heap_free_block ((uint8_t *) bytecode_data.opcodes); -} diff --git a/jerry-core/parser/js/deserializer.h b/jerry-core/parser/js/deserializer.h deleted file mode 100644 index 9fbb32fb0..000000000 --- a/jerry-core/parser/js/deserializer.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright 2014-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. - */ - -#ifndef DESERIALIZER_H -#define DESERIALIZER_H - -#include "jrt.h" -#include "ecma-globals.h" -#include "opcodes.h" -#include "scopes-tree.h" -#include "literal.h" - -void deserializer_init (void); -void deserializer_set_strings_buffer (const ecma_char_t *); -literal deserialize_literal_by_id (literal_index_t); -literal_index_t deserialize_lit_id_by_uid (uint8_t, opcode_counter_t); -const void *deserialize_bytecode (void); -opcode_t deserialize_opcode (opcode_counter_t); -op_meta deserialize_op_meta (opcode_counter_t); -uint8_t deserialize_min_temp (void); -void deserializer_free (void); - -#endif //DESERIALIZER_H diff --git a/jerry-core/parser/js/opcodes-dumper.cpp b/jerry-core/parser/js/opcodes-dumper.cpp index ab0544115..b8c0e629e 100644 --- a/jerry-core/parser/js/opcodes-dumper.cpp +++ b/jerry-core/parser/js/opcodes-dumper.cpp @@ -15,13 +15,13 @@ #include "opcodes-dumper.h" #include "serializer.h" -#include "deserializer.h" #include "jrt.h" #include "lexer.h" #include "stack.h" #include "syntax-errors.h" #include "jrt-libc-includes.h" #include "opcodes-native-call.h" +#include "serializer.h" #define MIN_TEMP_NAME 128 static idx_t temp_name, max_temp_name; @@ -445,7 +445,7 @@ split_opcode_counter (opcode_counter_t oc, idx_t *id1, idx_t *id2) static op_meta last_dumped_op_meta (void) { - return deserialize_op_meta ((opcode_counter_t) (serializer_get_current_opcode_counter () - 1)); + return serializer_get_op_meta ((opcode_counter_t) (serializer_get_current_opcode_counter () - 1)); } static void @@ -1049,7 +1049,7 @@ dump_varg_header_for_rewrite (varg_list_type vlt, operand obj) operand rewrite_varg_header_set_args_count (uint8_t args_count) { - op_meta om = deserialize_op_meta (STACK_TOP (varg_headers)); + op_meta om = serializer_get_op_meta (STACK_TOP (varg_headers)); switch (om.op.op_idx) { case OPCODE (func_expr_n): @@ -1875,7 +1875,7 @@ rewrite_logical_and_checks (void) { for (uint8_t i = STACK_TOP (U8); i < STACK_SIZE (logical_and_checks); i++) { - op_meta jmp_op_meta = deserialize_op_meta (STACK_ELEMENT (logical_and_checks, i)); + op_meta jmp_op_meta = serializer_get_op_meta (STACK_ELEMENT (logical_and_checks, i)); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (is_false_jmp_down)); idx_t id1, id2; split_opcode_counter (get_diff_from (STACK_ELEMENT (logical_and_checks, i)), &id1, &id2); @@ -1919,7 +1919,7 @@ rewrite_logical_or_checks (void) { for (uint8_t i = STACK_TOP (U8); i < STACK_SIZE (logical_or_checks); i++) { - op_meta jmp_op_meta = deserialize_op_meta (STACK_ELEMENT (logical_or_checks, i)); + op_meta jmp_op_meta = serializer_get_op_meta (STACK_ELEMENT (logical_or_checks, i)); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (is_true_jmp_down)); idx_t id1, id2; split_opcode_counter (get_diff_from (STACK_ELEMENT (logical_or_checks, i)), &id1, &id2); @@ -1955,7 +1955,7 @@ dump_conditional_check_for_rewrite (operand op) void rewrite_conditional_check (void) { - op_meta jmp_op_meta = deserialize_op_meta (STACK_TOP (conditional_checks)); + op_meta jmp_op_meta = serializer_get_op_meta (STACK_TOP (conditional_checks)); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (is_false_jmp_down)); idx_t id1, id2; split_opcode_counter (get_diff_from (STACK_TOP (conditional_checks)), &id1, &id2); @@ -1976,7 +1976,7 @@ dump_jump_to_end_for_rewrite (void) void rewrite_jump_to_end (void) { - op_meta jmp_op_meta = deserialize_op_meta (STACK_TOP (jumps_to_end)); + op_meta jmp_op_meta = serializer_get_op_meta (STACK_TOP (jumps_to_end)); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (jmp_down)); idx_t id1, id2; split_opcode_counter (get_diff_from (STACK_TOP (jumps_to_end)), &id1, &id2); @@ -2167,7 +2167,7 @@ rewrite_breaks (void) { idx_t id1, id2; split_opcode_counter ((opcode_counter_t) (break_target - break_oc), &id1, &id2); - op_meta break_op_meta = deserialize_op_meta (break_oc); + op_meta break_op_meta = serializer_get_op_meta (break_oc); JERRY_ASSERT (break_op_meta.op.op_idx == OPCODE (jmp_down)); break_op_meta.op.data.jmp_down.opcode_1 = id1; break_op_meta.op.data.jmp_down.opcode_2 = id2; @@ -2189,7 +2189,7 @@ rewrite_continues (void) { idx_t id1, id2; split_opcode_counter ((opcode_counter_t) (continue_target - continue_oc), &id1, &id2); - op_meta continue_op_meta = deserialize_op_meta (continue_oc); + op_meta continue_op_meta = serializer_get_op_meta (continue_oc); JERRY_ASSERT (continue_op_meta.op.op_idx == OPCODE (jmp_down)); continue_op_meta.op.data.jmp_down.opcode_1 = id1; continue_op_meta.op.data.jmp_down.opcode_2 = id2; @@ -2233,7 +2233,7 @@ rewrite_case_clause (void) const opcode_counter_t jmp_oc = STACK_ELEMENT (case_clauses, STACK_HEAD (U8, 2)); idx_t id1, id2; split_opcode_counter (get_diff_from (jmp_oc), &id1, &id2); - op_meta jmp_op_meta = deserialize_op_meta (jmp_oc); + op_meta jmp_op_meta = serializer_get_op_meta (jmp_oc); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (is_true_jmp_down)); jmp_op_meta.op.data.is_true_jmp_down.opcode_1 = id1; jmp_op_meta.op.data.is_true_jmp_down.opcode_2 = id2; @@ -2247,7 +2247,7 @@ rewrite_default_clause (void) const opcode_counter_t jmp_oc = STACK_TOP (case_clauses); idx_t id1, id2; split_opcode_counter (get_diff_from (jmp_oc), &id1, &id2); - op_meta jmp_op_meta = deserialize_op_meta (jmp_oc); + op_meta jmp_op_meta = serializer_get_op_meta (jmp_oc); JERRY_ASSERT (jmp_op_meta.op.op_idx == OPCODE (jmp_down)); jmp_op_meta.op.data.jmp_down.opcode_1 = id1; jmp_op_meta.op.data.jmp_down.opcode_2 = id2; @@ -2286,7 +2286,7 @@ dump_try_for_rewrite (void) void rewrite_try (void) { - op_meta try_op_meta = deserialize_op_meta (STACK_TOP (tries)); + op_meta try_op_meta = serializer_get_op_meta (STACK_TOP (tries)); JERRY_ASSERT (try_op_meta.op.op_idx == OPCODE (try_block)); idx_t id1, id2; split_opcode_counter (get_diff_from (STACK_TOP (tries)), &id1, &id2); @@ -2310,7 +2310,7 @@ dump_catch_for_rewrite (operand op) void rewrite_catch (void) { - op_meta catch_op_meta = deserialize_op_meta (STACK_TOP (catches)); + op_meta catch_op_meta = serializer_get_op_meta (STACK_TOP (catches)); JERRY_ASSERT (catch_op_meta.op.op_idx == OPCODE (meta) && catch_op_meta.op.data.meta.type == OPCODE_META_TYPE_CATCH); idx_t id1, id2; @@ -2332,7 +2332,7 @@ dump_finally_for_rewrite (void) void rewrite_finally (void) { - op_meta finally_op_meta = deserialize_op_meta (STACK_TOP (finallies)); + op_meta finally_op_meta = serializer_get_op_meta (STACK_TOP (finallies)); JERRY_ASSERT (finally_op_meta.op.op_idx == OPCODE (meta) && finally_op_meta.op.data.meta.type == OPCODE_META_TYPE_FINALLY); idx_t id1, id2; @@ -2363,7 +2363,7 @@ dumper_variable_declaration_exists (literal_index_t lit_id) for (opcode_counter_t oc = (opcode_counter_t) (serializer_get_current_opcode_counter () - 1); oc > 0; oc--) { - const op_meta var_decl_op_meta = deserialize_op_meta (oc); + const op_meta var_decl_op_meta = serializer_get_op_meta (oc); if (var_decl_op_meta.op.op_idx != OPCODE (var_decl)) { break; @@ -2407,7 +2407,7 @@ void rewrite_reg_var_decl (void) { opcode_counter_t reg_var_decl_oc = STACK_TOP (reg_var_decls); - op_meta opm = deserialize_op_meta (reg_var_decl_oc); + op_meta opm = serializer_get_op_meta (reg_var_decl_oc); JERRY_ASSERT (opm.op.op_idx == OPCODE (reg_var_decl)); opm.op.data.reg_var_decl.max = max_temp_name; serializer_rewrite_op_meta (reg_var_decl_oc, opm); diff --git a/jerry-core/parser/js/parser.cpp b/jerry-core/parser/js/parser.cpp index e3186bf8c..a98573dc4 100644 --- a/jerry-core/parser/js/parser.cpp +++ b/jerry-core/parser/js/parser.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "jrt-libc-includes.h" #include "parser.h" #include "opcodes.h" @@ -20,13 +22,12 @@ #include "vm.h" #include "stack.h" #include "hash-table.h" -#include "deserializer.h" #include "opcodes-native-call.h" #include "scopes-tree.h" #include "ecma-helpers.h" #include "syntax-errors.h" #include "opcodes-dumper.h" -#include "stdarg.h" +#include "serializer.h" #define NESTING_ITERATIONAL 1 #define NESTING_SWITCH 2 @@ -2713,7 +2714,7 @@ parser_parse_program (void) serializer_dump_literals (lexer_get_literals (), lexer_get_literals_count ()); serializer_merge_scopes_into_bytecode (); serializer_set_scope (NULL); - deserializer_set_strings_buffer (lexer_get_strings_cache ()); + serializer_set_strings_buffer (lexer_get_strings_cache ()); scopes_tree_free (STACK_TOP (scopes)); STACK_DROP (scopes, 1); @@ -2723,7 +2724,7 @@ void parser_init (const char *source, size_t source_size, bool show_opcodes) { lexer_init (source, source_size, show_opcodes); - serializer_init (show_opcodes); + serializer_set_show_opcodes (show_opcodes); dumper_init (); syntax_init (); @@ -2739,6 +2740,5 @@ parser_free (void) syntax_free (); dumper_free (); - serializer_free (); lexer_free (); } diff --git a/jerry-core/parser/js/scopes-tree.cpp b/jerry-core/parser/js/scopes-tree.cpp index 6b7898880..11637894c 100644 --- a/jerry-core/parser/js/scopes-tree.cpp +++ b/jerry-core/parser/js/scopes-tree.cpp @@ -14,10 +14,10 @@ */ #include "scopes-tree.h" +#include "bytecode-data.h" #include "mem-heap.h" #include "jrt-libc-includes.h" #include "lexer.h" -#include "bytecode-data.h" #define OPCODE(op) (__op__idx_##op) #define HASH_SIZE 128 diff --git a/jerry-core/parser/js/scopes-tree.h b/jerry-core/parser/js/scopes-tree.h index e5d32cc75..350765fd0 100644 --- a/jerry-core/parser/js/scopes-tree.h +++ b/jerry-core/parser/js/scopes-tree.h @@ -1,4 +1,4 @@ -/* Copyright 2014 Samsung Electronics Co., Ltd. +/* Copyright 2014-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. @@ -16,7 +16,6 @@ #ifndef SCOPES_TREE_H #define SCOPES_TREE_H -#include "tree.h" #include "linked-list.h" #include "lexer.h" #include "ecma-globals.h" @@ -33,6 +32,14 @@ typedef struct } op_meta; +typedef struct tree_header +{ + struct tree_header *parent; + linked_list children; + uint8_t children_num; +} +tree_header; + typedef struct { tree_header t; diff --git a/jerry-core/parser/js/serializer.cpp b/jerry-core/parser/js/serializer.cpp index 5dff032d9..b6eec71a5 100644 --- a/jerry-core/parser/js/serializer.cpp +++ b/jerry-core/parser/js/serializer.cpp @@ -13,19 +13,67 @@ * limitations under the License. */ -#include "jrt.h" #include "serializer.h" +#include "bytecode-data.h" +#include "jrt.h" #include "parser.h" #include "jrt-libc-includes.h" -#include "bytecode-data.h" -#include "deserializer.h" #include "pretty-printer.h" -bytecode_data_t bytecode_data; -scopes_tree current_scope; +static bytecode_data_t bytecode_data; +static scopes_tree current_scope; static bool print_opcodes; +op_meta +serializer_get_op_meta (opcode_counter_t oc) +{ + JERRY_ASSERT (current_scope); + return scopes_tree_op_meta (current_scope, oc); +} + +opcode_t +serializer_get_opcode (opcode_counter_t oc) +{ + if (bytecode_data.opcodes == NULL) + { + return serializer_get_op_meta (oc).op; + } + JERRY_ASSERT (oc < bytecode_data.opcodes_count); + return bytecode_data.opcodes[oc]; +} + +literal +serializer_get_literal_by_id (literal_index_t id) +{ + JERRY_ASSERT (id != INVALID_LITERAL); + JERRY_ASSERT (id < bytecode_data.literals_count); + return bytecode_data.literals[id]; +} + +literal_index_t +serializer_get_literal_id_by_uid (uint8_t id, opcode_counter_t oc) +{ + if (bytecode_data.lit_id_hash == null_hash) + { + return INVALID_LITERAL; + } + return lit_id_hash_table_lookup (bytecode_data.lit_id_hash, id, oc); +} + +const void * +serializer_get_bytecode (void) +{ + JERRY_ASSERT (bytecode_data.opcodes != NULL); + return bytecode_data.opcodes; +} + +void +serializer_set_strings_buffer (const ecma_char_t *s) +{ + bytecode_data.strings_buffer = s; +} + void serializer_set_scope (scopes_tree new_scope) { @@ -43,7 +91,7 @@ serializer_merge_scopes_into_bytecode (void) } void -serializer_dump_literals (const literal literals[], literal_index_t literals_count) +serializer_dump_literals (const literal *literals, literal_index_t literals_count) { #ifdef JERRY_ENABLE_PRETTY_PRINTER if (print_opcodes) @@ -131,13 +179,36 @@ serializer_print_opcodes (void) } void -serializer_init (bool show_opcodes) +serializer_init () { current_scope = NULL; + print_opcodes = false; + + bytecode_data.strings_buffer = NULL; + bytecode_data.literals = NULL; + bytecode_data.opcodes = NULL; + bytecode_data.lit_id_hash = null_hash; +} + +void serializer_set_show_opcodes (bool show_opcodes) +{ print_opcodes = show_opcodes; } void serializer_free (void) { + if (bytecode_data.strings_buffer) + { + mem_heap_free_block ((uint8_t *) bytecode_data.strings_buffer); + } + if (bytecode_data.lit_id_hash != null_hash) + { + lit_id_hash_table_free (bytecode_data.lit_id_hash); + } + if (bytecode_data.literals != NULL) + { + mem_heap_free_block ((uint8_t *) bytecode_data.literals); + } + mem_heap_free_block ((uint8_t *) bytecode_data.opcodes); } diff --git a/jerry-core/parser/js/serializer.h b/jerry-core/parser/js/serializer.h index 214c45250..dd06aa4e9 100644 --- a/jerry-core/parser/js/serializer.h +++ b/jerry-core/parser/js/serializer.h @@ -23,7 +23,14 @@ #include "literal.h" #include "scopes-tree.h" -void serializer_init (bool show_opcodes); +void serializer_init (); +void serializer_set_show_opcodes (bool show_opcodes); +op_meta serializer_get_op_meta (opcode_counter_t); +opcode_t serializer_get_opcode (opcode_counter_t); +literal serializer_get_literal_by_id (literal_index_t); +literal_index_t serializer_get_literal_id_by_uid (uint8_t, opcode_counter_t); +const void *serializer_get_bytecode (void); +void serializer_set_strings_buffer (const ecma_char_t *); void serializer_dump_literals (const literal *, literal_index_t); void serializer_set_scope (scopes_tree); void serializer_merge_scopes_into_bytecode (void); diff --git a/jerry-core/vm/opcodes-ecma-support.h b/jerry-core/vm/opcodes-ecma-support.h index 481a67660..80052a66f 100644 --- a/jerry-core/vm/opcodes-ecma-support.h +++ b/jerry-core/vm/opcodes-ecma-support.h @@ -30,7 +30,7 @@ #include "ecma-objects-general.h" #include "ecma-reference.h" #include "ecma-try-catch-macro.h" -#include "deserializer.h" +#include "serializer.h" bool is_reg_variable (int_data_t *int_data, idx_t var_idx); ecma_completion_value_t get_variable_value (int_data_t *, idx_t, bool); diff --git a/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp b/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp index 594273c68..0980292bb 100644 --- a/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp +++ b/jerry-core/vm/opcodes-ecma-try-catch-finally.cpp @@ -62,8 +62,9 @@ opfunc_try_block (opcode_t opdata, /**< operation data */ JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER); - const literal_index_t catch_exc_val_var_name_lit_idx = deserialize_lit_id_by_uid (next_opcode.data.meta.data_1, - int_data->pos); + const literal_index_t catch_exc_val_var_name_lit_idx = serializer_get_literal_id_by_uid ( + next_opcode.data.meta.data_1, + int_data->pos); int_data->pos++; ecma_string_t *catch_exc_var_name_str_p = ecma_new_ecma_string_from_lit_index (catch_exc_val_var_name_lit_idx); diff --git a/jerry-core/vm/opcodes-helpers-variables.cpp b/jerry-core/vm/opcodes-helpers-variables.cpp index b82889e77..6f0156cf7 100644 --- a/jerry-core/vm/opcodes-helpers-variables.cpp +++ b/jerry-core/vm/opcodes-helpers-variables.cpp @@ -91,7 +91,7 @@ get_variable_value (int_data_t *int_data, /**< interpreter context */ else { ecma_string_t var_name_string; - const literal_index_t lit_id = deserialize_lit_id_by_uid (var_idx, int_data->pos); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (var_idx, int_data->pos); JERRY_ASSERT (lit_id != INVALID_LITERAL); ecma_new_ecma_string_on_stack_from_lit_index (&var_name_string, lit_id); @@ -158,7 +158,7 @@ set_variable_value (int_data_t *int_data, /**< interpreter context */ else { ecma_string_t var_name_string; - const literal_index_t lit_id = deserialize_lit_id_by_uid (var_idx, lit_oc); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (var_idx, lit_oc); JERRY_ASSERT (lit_id != INVALID_LITERAL); ecma_new_ecma_string_on_stack_from_lit_index (&var_name_string, lit_id); diff --git a/jerry-core/vm/opcodes-varg.cpp b/jerry-core/vm/opcodes-varg.cpp index 61f8fd4f0..dcaee0cea 100644 --- a/jerry-core/vm/opcodes-varg.cpp +++ b/jerry-core/vm/opcodes-varg.cpp @@ -96,7 +96,8 @@ fill_params_list (int_data_t *int_data, /**< interpreter context */ JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_VARG); - const literal_index_t param_name_lit_idx = deserialize_lit_id_by_uid (next_opcode.data.meta.data_1, int_data->pos); + const literal_index_t param_name_lit_idx = serializer_get_literal_id_by_uid (next_opcode.data.meta.data_1, + int_data->pos); params_names [param_index] = ecma_new_ecma_string_from_lit_index (param_name_lit_idx); diff --git a/jerry-core/vm/opcodes.cpp b/jerry-core/vm/opcodes.cpp index 8f74d2e77..2c82191cd 100644 --- a/jerry-core/vm/opcodes.cpp +++ b/jerry-core/vm/opcodes.cpp @@ -13,11 +13,11 @@ * limitations under the License. */ -#include "deserializer.h" #include "jrt.h" -#include "vm.h" #include "opcodes.h" #include "opcodes-ecma-support.h" +#include "serializer.h" +#include "vm.h" /** * Note: @@ -98,7 +98,7 @@ opfunc_assignment (opcode_t opdata, /**< operation data */ } else if (type_value_right == OPCODE_ARG_TYPE_STRING) { - const literal_index_t lit_id = deserialize_lit_id_by_uid (src_val_descr, int_data->pos); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (src_val_descr, int_data->pos); ecma_string_t *string_p = ecma_new_ecma_string_from_lit_index (lit_id); ret_value = set_variable_value (int_data, @@ -127,8 +127,8 @@ opfunc_assignment (opcode_t opdata, /**< operation data */ { ecma_number_t *num_p = int_data->tmp_num_p; - const literal_index_t lit_id = deserialize_lit_id_by_uid (src_val_descr, int_data->pos); - const literal lit = deserialize_literal_by_id (lit_id); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (src_val_descr, int_data->pos); + const literal lit = serializer_get_literal_by_id (lit_id); JERRY_ASSERT (lit.type == LIT_NUMBER); *num_p = lit.data.num; @@ -142,8 +142,8 @@ opfunc_assignment (opcode_t opdata, /**< operation data */ { ecma_number_t *num_p = int_data->tmp_num_p; - const literal_index_t lit_id = deserialize_lit_id_by_uid (src_val_descr, int_data->pos); - const literal lit = deserialize_literal_by_id (lit_id); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (src_val_descr, int_data->pos); + const literal lit = serializer_get_literal_by_id (lit_id); JERRY_ASSERT (lit.type == LIT_NUMBER); *num_p = ecma_number_negate (lit.data.num); @@ -397,8 +397,8 @@ ecma_completion_value_t opfunc_var_decl (opcode_t opdata, /**< operation data */ int_data_t *int_data) /**< interpreter context */ { - const literal_index_t lit_id = deserialize_lit_id_by_uid (opdata.data.var_decl.variable_name, - int_data->pos); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (opdata.data.var_decl.variable_name, + int_data->pos); JERRY_ASSERT (lit_id != INVALID_LITERAL); ecma_string_t *var_name_string_p = ecma_new_ecma_string_from_lit_index (lit_id); @@ -487,8 +487,8 @@ opfunc_func_decl_n (opcode_t opdata, /**< operation data */ const idx_t function_name_idx = opdata.data.func_decl_n.name_lit_idx; const ecma_length_t params_number = opdata.data.func_decl_n.arg_list; - literal_index_t function_name_lit_id = deserialize_lit_id_by_uid (function_name_idx, - int_data->pos); + literal_index_t function_name_lit_id = serializer_get_literal_id_by_uid (function_name_idx, + int_data->pos); int_data->pos++; @@ -563,7 +563,7 @@ opfunc_func_expr_n (opcode_t opdata, /**< operation data */ { scope_p = ecma_create_decl_lex_env (int_data->lex_env_p); - const literal_index_t lit_id = deserialize_lit_id_by_uid (function_name_lit_idx, lit_oc); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (function_name_lit_idx, lit_oc); JERRY_ASSERT (lit_id != INVALID_LITERAL); function_name_string_p = ecma_new_ecma_string_from_lit_index (lit_id); @@ -1389,7 +1389,7 @@ evaluate_arg_for_typeof (int_data_t *int_data, /**< interpreter context */ } else { - const literal_index_t lit_id = deserialize_lit_id_by_uid (var_idx, int_data->pos); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (var_idx, int_data->pos); JERRY_ASSERT (lit_id != INVALID_LITERAL); ecma_string_t *var_name_string_p = ecma_new_ecma_string_from_lit_index (lit_id); @@ -1505,7 +1505,7 @@ opfunc_delete_var (opcode_t opdata, /**< operation data */ ecma_completion_value_t ret_value; - const literal_index_t lit_id = deserialize_lit_id_by_uid (name_lit_idx, lit_oc); + const literal_index_t lit_id = serializer_get_literal_id_by_uid (name_lit_idx, lit_oc); JERRY_ASSERT (lit_id != INVALID_LITERAL); ecma_string_t *name_string_p = ecma_new_ecma_string_from_lit_index (lit_id); diff --git a/jerry-core/vm/pretty-printer.cpp b/jerry-core/vm/pretty-printer.cpp index 120e2b644..d83ece457 100644 --- a/jerry-core/vm/pretty-printer.cpp +++ b/jerry-core/vm/pretty-printer.cpp @@ -13,16 +13,16 @@ * limitations under the License. */ -#include "jrt.h" #ifdef JERRY_ENABLE_PRETTY_PRINTER +#include + #include "pretty-printer.h" #include "jrt-libc-includes.h" #include "lexer.h" -#include "deserializer.h" #include "opcodes-native-call.h" #include "ecma-helpers.h" #include "ecma-globals.h" -#include +#include "serializer.h" #define NAME_TO_ID(op) (__op__idx_##op) @@ -79,7 +79,7 @@ dump_literal (literal lit) } void -pp_literals (const literal lits[], literal_index_t size) +pp_literals (const literal *lits, literal_index_t size) { printf ("LITERALS %lu:\n", (unsigned long) size); for (literal_index_t i = 0; i < size; i++) @@ -160,7 +160,7 @@ var_to_str (opcode_t opcode, literal_index_t lit_ids[], opcode_counter_t oc, uin } else { - return lit_id_to_str (deserialize_lit_id_by_uid (raw.uids[current_arg], oc)); + return lit_id_to_str (serializer_get_literal_id_by_uid (raw.uids[current_arg], oc)); } } @@ -241,7 +241,7 @@ pp_op_meta (opcode_counter_t oc, op_meta opm, bool rewrite) { PP_OP (addition, "%s = %s + %s;"); PP_OP (substraction, "%s = %s - %s;"); - PP_OP (division, "%s = %s - %s;"); + PP_OP (division, "%s = %s / %s;"); PP_OP (multiplication, "%s = %s * %s;"); PP_OP (remainder, "%s = %s %% %s;"); PP_OP (unary_minus, "%s = -%s;"); @@ -447,7 +447,7 @@ pp_op_meta (opcode_counter_t oc, op_meta opm, bool rewrite) while ((int16_t) start >= 0 && !found) { start--; - switch (deserialize_opcode (start).op_idx) + switch (serializer_get_opcode (start).op_idx) { case NAME_TO_ID (call_n): case NAME_TO_ID (native_call): @@ -462,7 +462,7 @@ pp_op_meta (opcode_counter_t oc, op_meta opm, bool rewrite) } } } - opcode_t start_op = deserialize_opcode (start); + opcode_t start_op = serializer_get_opcode (start); switch (start_op.op_idx) { case NAME_TO_ID (call_n): @@ -527,7 +527,7 @@ pp_op_meta (opcode_counter_t oc, op_meta opm, bool rewrite) } for (opcode_counter_t counter = start; counter <= oc; counter++) { - opcode_t meta_op = deserialize_opcode (counter); + opcode_t meta_op = serializer_get_opcode (counter); switch (meta_op.op_idx) { case NAME_TO_ID (meta): diff --git a/tests/unit/test_preparser.cpp b/tests/unit/test_preparser.cpp index dad12efd5..0781fed88 100644 --- a/tests/unit/test_preparser.cpp +++ b/tests/unit/test_preparser.cpp @@ -18,9 +18,9 @@ #include "jrt.h" #include "mem-allocator.h" #include "opcodes.h" -#include "deserializer.h" #include "common.h" #include "parser.h" +#include "serializer.h" /** * Unit test's main function. @@ -33,7 +33,7 @@ main (int __attr_unused___ argc, bool is_ok; mem_init (); - deserializer_init (); + serializer_init (); parser_init (program, strlen (program), true); parser_parse_program (); parser_free (); @@ -47,7 +47,7 @@ main (int __attr_unused___ argc, getop_exitval (0) // exit 0; }; - if (!opcodes_equal ((const opcode_t *) deserialize_bytecode (), opcodes, 5)) + if (!opcodes_equal ((const opcode_t *) serializer_get_bytecode (), opcodes, 5)) { is_ok = false; } @@ -56,7 +56,7 @@ main (int __attr_unused___ argc, is_ok = true; } - deserializer_free (); + serializer_free (); mem_finalize (false); return (is_ok ? 0 : 1);