Ilmir Usmanov dc8ab27900 Split parser into parser itself, opcodes dumper and syntax errors checker.
Add internal hash map of literal indexes:
  In this hash map key is pair of block number and literal's unique identifier in the block, and the value is a literal index that unique in the whole program.
  Block is a continues array of opcodes. So, bytecode is splitted into blocks.
  Each block has its own uid counter. To get literal index the interpreter looks up it in the hash map.
  Thus, now JS program is able to have more than 255 identifiers/string literals.
  The first 128 (0-127) uids are reserved for block's uid counter, the other 128 (128-255) are reserved for tmp variables.
2014-12-10 18:31:59 +03:00

150 lines
3.4 KiB
C

/* 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 "globals.h"
#include "serializer.h"
#include "parser.h"
#include "jerry-libc.h"
#include "bytecode-data.h"
#include "deserializer.h"
#include "pretty-printer.h"
static bool print_opcodes;
void
serializer_set_scope (scopes_tree new_scope)
{
current_scope = new_scope;
}
static uint16_t
hash_function (void *raw_key)
{
lit_id_table_key *key = (lit_id_table_key *) raw_key;
JERRY_ASSERT (bytecode_data.opcodes_count > 0);
return (uint16_t) (key->oc + key->uid) & ((1u << CONFIG_LITERAL_HASH_TABLE_KEY_BITS) - 1);
}
void
serializer_merge_scopes_into_bytecode (void)
{
JERRY_ASSERT (bytecode_data.lit_id_hash == null_hash);
bytecode_data.opcodes_count = scopes_tree_count_opcodes (current_scope);
bytecode_data.lit_id_hash = hash_table_init (sizeof (lit_id_table_key), sizeof (literal_index_t),
1u << CONFIG_LITERAL_HASH_TABLE_KEY_BITS, hash_function,
MEM_HEAP_ALLOC_LONG_TERM);
bytecode_data.opcodes = scopes_tree_raw_data (current_scope, bytecode_data.lit_id_hash);
}
void
serializer_dump_literals (const literal literals[], literal_index_t literals_count)
{
#ifdef JERRY_ENABLE_PP
if (print_opcodes)
{
pp_literals (literals, literals_count);
}
#endif
bytecode_data.literals_count = literals_count;
bytecode_data.literals = literals;
}
void
serializer_dump_op_meta (op_meta op)
{
JERRY_ASSERT (scopes_tree_opcodes_num (current_scope) < MAX_OPCODES);
scopes_tree_add_op_meta (current_scope, op);
#ifdef JERRY_ENABLE_PP
if (print_opcodes)
{
pp_op_meta ((opcode_counter_t) (scopes_tree_opcodes_num (current_scope) - 1), op, false);
}
#endif
}
opcode_counter_t
serializer_get_current_opcode_counter (void)
{
return scopes_tree_opcodes_num (current_scope);
}
opcode_counter_t
serializer_count_opcodes_in_subscopes (void)
{
return (opcode_counter_t) (scopes_tree_count_opcodes (current_scope) - scopes_tree_opcodes_num (current_scope));
}
void
serializer_set_writing_position (opcode_counter_t oc)
{
scopes_tree_set_opcodes_num (current_scope, oc);
}
void
serializer_rewrite_op_meta (const opcode_counter_t loc, op_meta op)
{
scopes_tree_set_op_meta (current_scope, loc, op);
#ifdef JERRY_ENABLE_PP
if (print_opcodes)
{
pp_op_meta (loc, op, true);
}
#endif
}
void
serializer_print_opcodes (void)
{
#ifdef JERRY_ENABLE_PP
opcode_counter_t loc;
if (!print_opcodes)
{
return;
}
__printf ("AFTER OPTIMIZER:\n");
for (loc = 0; loc < bytecode_data.opcodes_count; loc++)
{
const op_meta opm = (op_meta)
{
.op = bytecode_data.opcodes[loc],
.lit_id =
{
NOT_A_LITERAL
}
};
pp_op_meta (loc, opm, false);
}
#endif
}
void
serializer_init (bool show_opcodes)
{
current_scope = NULL;
print_opcodes = show_opcodes;
}
void
serializer_free (void)
{
}