Parser refactoring. Remove deserializer notion.

This commit is contained in:
Andrey Shitov
2015-04-07 13:05:09 +03:00
parent 469ef4e622
commit 7c67606b00
21 changed files with 176 additions and 317 deletions
@@ -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)
-62
View File
@@ -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 */
-29
View File
@@ -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 */
+14 -14
View File
@@ -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
-101
View File
@@ -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);
}
-35
View File
@@ -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
+16 -16
View File
@@ -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);
+5 -5
View File
@@ -13,6 +13,8 @@
* limitations under the License.
*/
#include <stdarg.h>
#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 ();
}
+1 -1
View File
@@ -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
+9 -2
View File
@@ -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;
+78 -7
View File
@@ -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);
}
+8 -1
View File
@@ -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);