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
This commit is contained in:
Andrey Shitov 2015-11-03 19:14:19 +03:00 committed by Ruben Ayrapetyan
parent b1de93abd6
commit 50d124bfc3
51 changed files with 9044 additions and 7401 deletions

View File

@ -112,6 +112,7 @@ project (JerryCore CXX C ASM)
${CMAKE_SOURCE_DIR}/jerry-core/ecma/base
${CMAKE_SOURCE_DIR}/jerry-core/ecma/operations
${CMAKE_SOURCE_DIR}/jerry-core/parser/js
${CMAKE_SOURCE_DIR}/jerry-core/parser/js/bc
${CMAKE_SOURCE_DIR}/jerry-core/parser/js/collections
${CMAKE_SOURCE_DIR}/jerry-core/parser/regexp
${CMAKE_SOURCE_DIR}/jerry-core/jrt)
@ -131,6 +132,7 @@ project (JerryCore CXX C ASM)
file(GLOB SOURCE_CORE_ECMA_BASE ecma/base/*.cpp)
file(GLOB SOURCE_CORE_ECMA_OPERATIONS ecma/operations/*.cpp)
file(GLOB SOURCE_CORE_PARSER_JS parser/js/*.cpp)
file(GLOB SOURCE_CORE_PARSER_JS_BC parser/js/bc/*.cpp)
file(GLOB SOURCE_CORE_PARSER_JS_COLLECTIONS parser/js/collections/*.cpp)
file(GLOB SOURCE_CORE_PARSER_REGEXP parser/regexp/*.cpp)
file(GLOB SOURCE_CORE_JRT jrt/*.cpp)
@ -145,6 +147,7 @@ project (JerryCore CXX C ASM)
${SOURCE_CORE_ECMA_BASE}
${SOURCE_CORE_ECMA_OPERATIONS}
${SOURCE_CORE_PARSER_JS}
${SOURCE_CORE_PARSER_JS_BC}
${SOURCE_CORE_PARSER_JS_COLLECTIONS}
${SOURCE_CORE_PARSER_REGEXP}
${SOURCE_CORE_JRT})

View File

@ -795,6 +795,8 @@ FIXME (Move to library that should define the type (literal.h /* ? */))
typedef rcs_record_t *literal_t;
typedef rcs_cpointer_t lit_cpointer_t;
#define NOT_A_LITERAL (lit_cpointer_t::null_cp ())
/**
* ECMA string-value descriptor
*/

View File

@ -20,6 +20,7 @@
* @{
*/
#include "bytecode-data.h"
#include "ecma-alloc.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
@ -29,7 +30,6 @@
#include "jrt-libc-includes.h"
#include "lit-char-helpers.h"
#include "lit-magic-strings.h"
#include "serializer.h"
#include "vm.h"
/**

View File

@ -21,7 +21,6 @@
#include "ecma-function-object.h"
#include "ecma-lex-env.h"
#include "ecma-try-catch-macro.h"
#include "serializer.h"
#include "lit-magic-strings.h"
#include "parser.h"

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-eval.h"
@ -21,7 +22,6 @@
#include "ecma-helpers.h"
#include "ecma-lex-env.h"
#include "parser.h"
#include "serializer.h"
#include "vm.h"
/** \addtogroup ecma ECMA
@ -114,7 +114,7 @@ ecma_op_eval_chars_buffer (const jerry_api_char_t *code_p, /**< code characters
if (!code_contains_functions)
{
serializer_remove_bytecode_data (bytecode_data_p);
bc_remove_bytecode_data (bytecode_data_p);
}
}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "ecma-alloc.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
@ -255,15 +256,14 @@ ecma_op_create_function_object (ecma_collection_header_t *formal_params_collecti
bool is_no_lex_env = false;
vm_instr_counter_t instr_pos = first_instr_pos;
opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (bytecode_header_p, instr_pos++);
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT)
if (bytecode_header_p->is_strict)
{
is_strict_mode_code = true;
}
if ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER)
&& (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER))
if (!bytecode_header_p->is_ref_arguments_identifier
&& !bytecode_header_p->is_ref_eval_identifier)
{
/* the code doesn't use 'arguments' identifier
* and doesn't perform direct call to eval,
@ -271,12 +271,12 @@ ecma_op_create_function_object (ecma_collection_header_t *formal_params_collecti
do_instantiate_arguments_object = false;
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS)
if (bytecode_header_p->is_args_moved_to_regs)
{
is_arguments_moved_to_regs = true;
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV)
if (bytecode_header_p->is_no_lex_env)
{
is_no_lex_env = true;
}

View File

@ -38,8 +38,7 @@ jerry_is_abort_on_fail (void);
typedef struct
{
uint32_t lit_table_size; /**< size of literal table */
uint32_t bytecode_size; /**< size of instructions array */
uint32_t idx_to_lit_map_size; /** size of idx-to-lit map */
uint32_t scopes_num; /**< number of saved bytecode pieces in the snapshot */
uint32_t is_run_global : 1; /**< flag, indicating whether the snapshot
* was dumped as 'Global scope'-mode code (true)
* or as eval-mode code (false) */
@ -48,6 +47,6 @@ typedef struct
/**
* Jerry snapshot format version
*/
#define JERRY_SNAPSHOT_VERSION (1u)
#define JERRY_SNAPSHOT_VERSION (2u)
#endif /* !JERRY_INTERNAL_H */

View File

@ -15,6 +15,7 @@
#include <stdio.h>
#include "bytecode-data.h"
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-builtins.h"
@ -27,9 +28,9 @@
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-try-catch-macro.h"
#include "lit-literal.h"
#include "lit-magic-strings.h"
#include "parser.h"
#include "serializer.h"
#define JERRY_INTERNAL
#include "jerry-internal.h"
@ -1420,7 +1421,7 @@ jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */
jerry_make_api_available ();
mem_init ();
serializer_init ();
lit_init ();
ecma_init ();
} /* jerry_init */
@ -1435,7 +1436,8 @@ jerry_cleanup (void)
bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0);
ecma_finalize ();
serializer_free ();
lit_finalize ();
bc_finalize ();
mem_finalize (is_show_mem_stats);
vm_finalize ();
} /* jerry_cleanup */
@ -1684,11 +1686,11 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
size_t header_offset = buffer_write_offset;
if (buffer_write_offset + sizeof (jerry_snapshot_header_t) > buffer_size)
if (buffer_write_offset + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT) > buffer_size)
{
return 0;
}
buffer_write_offset += sizeof (jerry_snapshot_header_t);
buffer_write_offset += JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT);
lit_mem_to_snapshot_id_map_entry_t* lit_map_p = NULL;
uint32_t literals_num;
@ -1704,17 +1706,21 @@ jerry_parse_and_save_snapshot (const jerry_api_char_t* source_p, /**< script sou
return 0;
}
size_t bytecode_offset = sizeof (version) + sizeof (jerry_snapshot_header_t) + header.lit_table_size;
size_t bytecode_offset = (sizeof (version)
+ JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT)
+ header.lit_table_size);
JERRY_ASSERT (JERRY_ALIGNUP (bytecode_offset, MEM_ALIGNMENT) == bytecode_offset);
bool is_ok = serializer_dump_bytecode_with_idx_map (buffer_p,
buffer_size,
&buffer_write_offset,
bytecode_data_p,
lit_map_p,
literals_num,
&header.bytecode_size,
&header.idx_to_lit_map_size);
bool is_ok = bc_save_bytecode_data (buffer_p,
buffer_size,
&buffer_write_offset,
bytecode_data_p,
lit_map_p,
literals_num,
&header.scopes_num);
JERRY_ASSERT (header.scopes_num != 0);
if (lit_map_p != NULL)
{
@ -1782,12 +1788,12 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
}
const jerry_snapshot_header_t *header_p = (const jerry_snapshot_header_t *) (snapshot_data_p + snapshot_read);
if (snapshot_read + sizeof (jerry_snapshot_header_t) > snapshot_size)
if (snapshot_read + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT) > snapshot_size)
{
return JERRY_COMPLETION_CODE_INVALID_SNAPSHOT_FORMAT;
}
snapshot_read += sizeof (jerry_snapshot_header_t);
snapshot_read += JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), MEM_ALIGNMENT);
if (snapshot_read + header_p->lit_table_size > snapshot_size)
{
@ -1809,19 +1815,19 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
snapshot_read += header_p->lit_table_size;
if (snapshot_read + header_p->bytecode_size + header_p->idx_to_lit_map_size > snapshot_size)
if (snapshot_read > snapshot_size)
{
mem_heap_free_block (lit_map_p);
return JERRY_COMPLETION_CODE_INVALID_SNAPSHOT_FORMAT;
}
const bytecode_data_header_t *bytecode_data_p;
bytecode_data_p = serializer_load_bytecode_with_idx_map (snapshot_data_p + snapshot_read,
header_p->bytecode_size,
header_p->idx_to_lit_map_size,
lit_map_p,
literals_num,
is_copy);
bytecode_data_p = bc_load_bytecode_data (snapshot_data_p + snapshot_read,
snapshot_size - snapshot_read,
lit_map_p,
literals_num,
is_copy,
header_p->scopes_num);
if (lit_map_p != NULL)
{

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "ecma-helpers.h"
#include "jrt.h"
#include "lit-literal-storage.h"
@ -666,26 +667,16 @@ lit_dump_literals_for_snapshot (uint8_t *buffer_p, /**< output snapshot buffer *
}
}
uint32_t aligned_size = JERRY_ALIGNUP (lit_table_size, MEM_ALIGNMENT);
if (aligned_size != lit_table_size)
if (!bc_align_data_in_output_buffer (&lit_table_size,
buffer_p,
buffer_size,
in_out_buffer_offset_p))
{
JERRY_ASSERT (aligned_size > lit_table_size);
uint8_t padding = 0;
uint32_t padding_bytes_num = (uint32_t) (aligned_size - lit_table_size);
for (uint32_t i = 0; i < padding_bytes_num; i++)
{
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, padding))
{
return false;
}
}
return false;
}
*out_map_num_p = literals_num;
*out_lit_table_size_p = aligned_size;
*out_lit_table_size_p = lit_table_size;
return true;
} /* lit_dump_literals_for_snapshot */

View File

@ -0,0 +1,827 @@
/* Copyright 2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* 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 "bytecode-data.h"
#include "pretty-printer.h"
#include "opcodes-dumper.h"
/**
* First node of the list of bytecodes
*/
static bytecode_data_header_t *first_bytecode_header_p = NULL;
/**
* Bytecode header in snapshot
*/
typedef struct
{
uint32_t size; /**< size of this bytecode data record */
uint32_t instrs_size; /**< size of instructions array */
uint32_t idx_to_lit_map_size; /**< size of idx-to-lit map */
uint32_t func_scopes_count; /**< count of function scopes inside current scope */
uint32_t var_decls_count; /**< count of variable declarations insdie current scope */
uint8_t is_strict : 1; /**< code is strict mode code */
uint8_t is_ref_arguments_identifier : 1; /**< code doesn't reference 'arguments' identifier */
uint8_t is_ref_eval_identifier : 1; /**< code doesn't reference 'eval' identifier */
uint8_t is_args_moved_to_regs : 1; /**< the function's arguments are moved to registers,
* so should be initialized in vm registers,
* and not in lexical environment */
uint8_t is_no_lex_env : 1; /**< no lex. env. is necessary for the scope */
} jerry_snapshot_bytecode_header_t;
/**
* Fill the fields of bytecode data header with specified values
*/
static void
bc_fill_bytecode_data_header (bytecode_data_header_t *bc_header_p, /**< byte-code scope data header to fill */
lit_id_hash_table *lit_id_hash_table_p, /**< (idx, block id) -> literal hash table */
vm_instr_t *bytecode_p, /**< byte-code instructions array */
mem_cpointer_t *declarations_p, /**< array of function / variable declarations */
uint16_t func_scopes_count, /**< number of function declarations / expressions
* located immediately in the corresponding scope */
uint16_t var_decls_count, /**< number of variable declarations immediately in the scope */
bool is_strict, /**< is the scope's code strict mode code? */
bool is_ref_arguments_identifier, /**< does the scope's code
* reference 'arguments' identifier? */
bool is_ref_eval_identifier, /**< does the scope's code
* reference 'eval' identifier? */
bool is_vars_and_args_to_regs_possible, /**< is it scope, for which variables / arguments
* can be moved to registers */
bool is_arguments_moved_to_regs, /**< is it function scope, for which arguments
* are located on registers, not in variables? */
bool is_no_lex_env) /**< is lexical environment unused in the scope? */
{
MEM_CP_SET_POINTER (bc_header_p->lit_id_hash_cp, lit_id_hash_table_p);
bc_header_p->instrs_p = bytecode_p;
bc_header_p->instrs_count = 0;
MEM_CP_SET_POINTER (bc_header_p->declarations_cp, declarations_p);
bc_header_p->func_scopes_count = func_scopes_count;
bc_header_p->var_decls_count = var_decls_count;
bc_header_p->next_header_cp = MEM_CP_NULL;
bc_header_p->is_strict = is_strict;
bc_header_p->is_ref_arguments_identifier = is_ref_arguments_identifier;
bc_header_p->is_ref_eval_identifier = is_ref_eval_identifier;
bc_header_p->is_vars_and_args_to_regs_possible = is_vars_and_args_to_regs_possible;
bc_header_p->is_args_moved_to_regs = is_arguments_moved_to_regs;
bc_header_p->is_no_lex_env = is_no_lex_env;
} /* bc_fill_bytecode_data_header */
/**
* Free memory occupied by bytecode data
*/
static void
bc_free_bytecode_data (bytecode_data_header_t *bytecode_data_p) /**< byte-code scope data header */
{
bytecode_data_header_t *next_to_handle_list_p = bytecode_data_p;
while (next_to_handle_list_p != NULL)
{
bytecode_data_header_t *bc_header_list_iter_p = next_to_handle_list_p;
next_to_handle_list_p = NULL;
while (bc_header_list_iter_p != NULL)
{
bytecode_data_header_t *header_p = bc_header_list_iter_p;
bc_header_list_iter_p = MEM_CP_GET_POINTER (bytecode_data_header_t, header_p->next_header_cp);
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t, header_p->declarations_cp);
for (uint32_t index = 0; index < header_p->func_scopes_count; index++)
{
bytecode_data_header_t *child_scope_header_p = MEM_CP_GET_NON_NULL_POINTER (bytecode_data_header_t,
declarations_p[index]);
JERRY_ASSERT (child_scope_header_p->next_header_cp == MEM_CP_NULL);
MEM_CP_SET_POINTER (child_scope_header_p->next_header_cp, next_to_handle_list_p);
next_to_handle_list_p = child_scope_header_p;
}
mem_heap_free_block (header_p);
}
JERRY_ASSERT (bc_header_list_iter_p == NULL);
}
} /* bc_free_bytecode_data */
/**
* Delete bytecode and associated hash table
*/
void
bc_remove_bytecode_data (const bytecode_data_header_t *bytecode_data_p) /**< byte-code scope data header */
{
bytecode_data_header_t *prev_header_p = NULL;
bytecode_data_header_t *cur_header_p = first_bytecode_header_p;
while (cur_header_p != NULL)
{
if (cur_header_p == bytecode_data_p)
{
if (prev_header_p)
{
prev_header_p->next_header_cp = cur_header_p->next_header_cp;
}
else
{
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
}
cur_header_p->next_header_cp = MEM_CP_NULL;
bc_free_bytecode_data (cur_header_p);
break;
}
prev_header_p = cur_header_p;
cur_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
}
} /* bc_remove_bytecode_data */
vm_instr_t bc_get_instr (const bytecode_data_header_t *bytecode_data_p, /**< byte-code scope data header */
vm_instr_counter_t oc) /**< instruction position */
{
JERRY_ASSERT (oc < bytecode_data_p->instrs_count);
return bytecode_data_p->instrs_p[oc];
}
/**
* Print bytecode instructions
*/
void
bc_print_instrs (const bytecode_data_header_t *bytecode_data_p) /**< byte-code scope data header */
{
#ifdef JERRY_ENABLE_PRETTY_PRINTER
for (vm_instr_counter_t loc = 0; loc < bytecode_data_p->instrs_count; loc++)
{
op_meta opm;
opm.op = bytecode_data_p->instrs_p[loc];
for (int i = 0; i < 3; i++)
{
opm.lit_id[i] = NOT_A_LITERAL;
}
pp_op_meta (bytecode_data_p, loc, opm, false);
}
#else
(void) bytecode_data_p;
#endif
} /* bc_print_instrs */
/**
* Dump single scopes tree into bytecode
*
* @return pointer to bytecode header of the outer most scope
*/
bytecode_data_header_t *
bc_dump_single_scope (scopes_tree scope_p) /**< a node of scopes tree */
{
const size_t entries_count = scope_p->max_uniq_literals_num;
const vm_instr_counter_t instrs_count = scopes_tree_instrs_num (scope_p);
const size_t blocks_count = JERRY_ALIGNUP (instrs_count, BLOCK_SIZE) / BLOCK_SIZE;
const size_t func_scopes_count = scopes_tree_child_scopes_num (scope_p);
const uint16_t var_decls_count = linked_list_get_length (scope_p->var_decls);
const size_t bytecode_size = JERRY_ALIGNUP (instrs_count * sizeof (vm_instr_t), MEM_ALIGNMENT);
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (entries_count, blocks_count);
const size_t declarations_area_size = JERRY_ALIGNUP (func_scopes_count * sizeof (mem_cpointer_t)
+ var_decls_count * sizeof (lit_cpointer_t),
MEM_ALIGNMENT);
const size_t header_and_tables_size = JERRY_ALIGNUP ((sizeof (bytecode_data_header_t)
+ hash_table_size
+ declarations_area_size),
MEM_ALIGNMENT);
uint8_t *buffer_p = (uint8_t *) mem_heap_alloc_block (bytecode_size + header_and_tables_size,
MEM_HEAP_ALLOC_LONG_TERM);
lit_id_hash_table *lit_id_hash_p = lit_id_hash_table_init (buffer_p + sizeof (bytecode_data_header_t),
hash_table_size,
entries_count, blocks_count);
mem_cpointer_t *declarations_p = (mem_cpointer_t *) (buffer_p + sizeof (bytecode_data_header_t) + hash_table_size);
for (size_t i = 0; i < func_scopes_count; i++)
{
declarations_p[i] = MEM_CP_NULL;
}
scopes_tree_dump_var_decls (scope_p, (lit_cpointer_t *) (declarations_p + func_scopes_count));
vm_instr_t *bytecode_p = (vm_instr_t *) (buffer_p + header_and_tables_size);
JERRY_ASSERT (scope_p->max_uniq_literals_num >= lit_id_hash_p->current_bucket_pos);
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
if ((uint16_t) func_scopes_count != func_scopes_count)
{
jerry_fatal (ERR_OUT_OF_MEMORY);
}
bc_fill_bytecode_data_header (header_p,
lit_id_hash_p, bytecode_p,
declarations_p,
(uint16_t) func_scopes_count,
var_decls_count,
scope_p->strict_mode,
scope_p->ref_arguments,
scope_p->ref_eval,
scope_p->is_vars_and_args_to_regs_possible,
false,
false);
JERRY_ASSERT (scope_p->bc_header_cp == MEM_CP_NULL);
MEM_CP_SET_NON_NULL_POINTER (scope_p->bc_header_cp, header_p);
return header_p;
} /* bc_dump_single_scope */
void
bc_register_root_bytecode_header (bytecode_data_header_t *bc_header_p)
{
MEM_CP_SET_POINTER (bc_header_p->next_header_cp, first_bytecode_header_p);
first_bytecode_header_p = bc_header_p;
} /* bc_register_root_bytecode_header */
/**
* Free all bytecode data which was allocated
*/
void
bc_finalize (void)
{
while (first_bytecode_header_p != NULL)
{
bytecode_data_header_t *header_p = first_bytecode_header_p;
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, header_p->next_header_cp);
header_p->next_header_cp = MEM_CP_NULL;
bc_free_bytecode_data (header_p);
}
} /* bc_finalize */
/**
* Convert literal id (operand value of instruction) to compressed pointer to literal
*
* Bytecode is divided into blocks of fixed size and each block has independent encoding of variable names,
* which are represented by 8 bit numbers - ids.
* This function performs conversion from id to literal.
*
* @return compressed pointer to literal
*/
lit_cpointer_t
bc_get_literal_cp_by_uid (uint8_t id, /**< literal idx */
const bytecode_data_header_t *bytecode_data_p, /**< pointer to bytecode */
vm_instr_counter_t oc) /**< position in the bytecode */
{
JERRY_ASSERT (bytecode_data_p);
lit_id_hash_table *lit_id_hash = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
if (lit_id_hash == NULL)
{
return INVALID_LITERAL;
}
return lit_id_hash_table_lookup (lit_id_hash, id, oc);
} /* bc_get_literal_cp_by_uid */
#ifdef JERRY_ENABLE_SNAPSHOT
/**
* Find literal offset in the table literal->offset
*/
uint32_t
bc_find_lit_offset (lit_cpointer_t lit_cp, /**< literal to find */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
* identifiers in
* literal storage
* to literal offsets
* in snapshot */
uint32_t literals_num) /**< number of entries in the map */
{
uint32_t lit_index;
for (lit_index = 0; lit_index < literals_num; lit_index++)
{
if (lit_map_p[lit_index].literal_id.packed_value == lit_cp.packed_value)
{
break;
}
}
JERRY_ASSERT (lit_index < literals_num);
return lit_map_p[lit_index].literal_offset;
} /* bc_find_lit_offset */
/**
* Write alignment bytes to outptut buffer to align 'in_out_size' to MEM_ALIGNEMENT
*
* @return true if alignment bytes were written successfully
* else otherwise
*/
bool
bc_align_data_in_output_buffer (uint32_t *in_out_size, /**< in: unaligned size, out: aligned size */
uint8_t *buffer_p, /**< buffer where to write */
size_t buffer_size, /**< buffer size */
size_t *in_out_buffer_offset_p) /**< current offset in buffer */
{
uint32_t aligned_size = JERRY_ALIGNUP (*in_out_size, MEM_ALIGNMENT);
if (aligned_size != (*in_out_size))
{
JERRY_ASSERT (aligned_size > (*in_out_size));
uint32_t padding_bytes_num = (uint32_t) (aligned_size - (*in_out_size));
uint8_t padding = 0;
for (uint32_t i = 0; i < padding_bytes_num; i++)
{
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, padding))
{
return false;
}
}
*in_out_size = aligned_size;
}
return true;
} /* bc_align_data_in_output_buffer */
/**
* Dump byte-code and idx-to-literal map of a single scope to snapshot
*
* @return true, upon success (i.e. buffer size is enough),
* false - otherwise.
*/
static bool
bc_save_bytecode_with_idx_map (uint8_t *buffer_p, /**< buffer to dump to */
size_t buffer_size, /**< buffer size */
size_t *in_out_buffer_offset_p, /**< in-out: buffer write offset */
const bytecode_data_header_t *bytecode_data_p, /**< byte-code data */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
* identifiers in
* literal storage
* to literal offsets
* in snapshot */
uint32_t literals_num) /**< literals number */
{
JERRY_ASSERT (JERRY_ALIGNUP (*in_out_buffer_offset_p, MEM_ALIGNMENT) == *in_out_buffer_offset_p);
jerry_snapshot_bytecode_header_t bytecode_header;
bytecode_header.func_scopes_count = bytecode_data_p->func_scopes_count;
bytecode_header.var_decls_count = bytecode_data_p->var_decls_count;
bytecode_header.is_strict = bytecode_data_p->is_strict;
bytecode_header.is_ref_arguments_identifier = bytecode_data_p->is_ref_arguments_identifier;
bytecode_header.is_ref_eval_identifier = bytecode_data_p->is_ref_eval_identifier;
bytecode_header.is_args_moved_to_regs = bytecode_data_p->is_args_moved_to_regs;
bytecode_header.is_no_lex_env = bytecode_data_p->is_no_lex_env;
size_t bytecode_header_offset = *in_out_buffer_offset_p;
/* Dump instructions */
*in_out_buffer_offset_p += JERRY_ALIGNUP (sizeof (jerry_snapshot_bytecode_header_t), MEM_ALIGNMENT);
vm_instr_counter_t instrs_num = bytecode_data_p->instrs_count;
const size_t instrs_array_size = sizeof (vm_instr_t) * instrs_num;
if (*in_out_buffer_offset_p + instrs_array_size > buffer_size)
{
return false;
}
memcpy (buffer_p + *in_out_buffer_offset_p, bytecode_data_p->instrs_p, instrs_array_size);
*in_out_buffer_offset_p += instrs_array_size;
bytecode_header.instrs_size = (uint32_t) (sizeof (vm_instr_t) * instrs_num);
/* Dump variable declarations */
mem_cpointer_t *func_scopes_p = MEM_CP_GET_POINTER (mem_cpointer_t, bytecode_data_p->declarations_cp);
lit_cpointer_t *var_decls_p = (lit_cpointer_t *) (func_scopes_p + bytecode_data_p->func_scopes_count);
uint32_t null_var_decls_num = 0;
for (uint32_t i = 0; i < bytecode_header.var_decls_count; ++i)
{
lit_cpointer_t lit_cp = var_decls_p[i];
if (lit_cp.packed_value == MEM_CP_NULL)
{
null_var_decls_num++;
continue;
}
uint32_t offset = bc_find_lit_offset (lit_cp, lit_map_p, literals_num);
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, offset))
{
return false;
}
}
bytecode_header.var_decls_count -= null_var_decls_num;
/* Dump uid->lit_cp hash table */
lit_id_hash_table *lit_id_hash_p = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
uint32_t idx_to_lit_map_size = lit_id_hash_table_dump_for_snapshot (buffer_p,
buffer_size,
in_out_buffer_offset_p,
lit_id_hash_p,
lit_map_p,
literals_num,
instrs_num);
if (idx_to_lit_map_size == 0)
{
return false;
}
bytecode_header.idx_to_lit_map_size = idx_to_lit_map_size;
/* Align to write next bytecode data at aligned address */
bytecode_header.size = (uint32_t) (*in_out_buffer_offset_p - bytecode_header_offset);
JERRY_ASSERT (bytecode_header.size == JERRY_ALIGNUP (sizeof (jerry_snapshot_bytecode_header_t), MEM_ALIGNMENT)
+ bytecode_header.instrs_size
+ bytecode_header.var_decls_count * sizeof (uint32_t)
+ idx_to_lit_map_size);
if (!bc_align_data_in_output_buffer (&bytecode_header.size,
buffer_p,
buffer_size,
in_out_buffer_offset_p))
{
return false;
}
/* Dump header at the saved offset */
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, &bytecode_header_offset, bytecode_header))
{
return false;
}
return true;
} /* bc_save_bytecode_with_idx_map */
/**
* Dump bytecode and summplementary data of all existing scopes to snapshot
*
* @return true if snapshot was dumped successfully
* false otherwise
*/
bool
bc_save_bytecode_data (uint8_t *buffer_p, /**< buffer to dump to */
size_t buffer_size, /**< buffer size */
size_t *in_out_buffer_offset_p, /**< in-out: buffer write offset */
const bytecode_data_header_t *bytecode_data_p, /**< byte-code data */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
* identifiers in
* literal storage
* to literal offsets
* in snapshot */
uint32_t literals_num, /**< literals number */
uint32_t *out_scopes_num) /**< number of scopes written */
{
bytecode_data_header_t *next_to_handle_list_p = first_bytecode_header_p;
while (next_to_handle_list_p != NULL)
{
if (next_to_handle_list_p == bytecode_data_p)
{
break;
}
next_to_handle_list_p = MEM_CP_GET_POINTER (bytecode_data_header_t, next_to_handle_list_p->next_header_cp);
}
JERRY_ASSERT (next_to_handle_list_p);
JERRY_ASSERT (next_to_handle_list_p->next_header_cp == MEM_CP_NULL);
*out_scopes_num = 0;
while (next_to_handle_list_p!= NULL)
{
bytecode_data_header_t *bc_header_list_iter_p = next_to_handle_list_p;
next_to_handle_list_p = NULL;
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t, bc_header_list_iter_p->declarations_cp);
if (!bc_save_bytecode_with_idx_map (buffer_p,
buffer_size,
in_out_buffer_offset_p,
bc_header_list_iter_p,
lit_map_p,
literals_num))
{
return false;
}
(*out_scopes_num)++;
next_to_handle_list_p = MEM_CP_GET_POINTER (bytecode_data_header_t, bc_header_list_iter_p->next_header_cp);
for (uint32_t index = bc_header_list_iter_p->func_scopes_count; index > 0 ; index--)
{
bytecode_data_header_t *child_scope_header_p = MEM_CP_GET_NON_NULL_POINTER (bytecode_data_header_t,
declarations_p[index-1]);
JERRY_ASSERT (child_scope_header_p->next_header_cp == MEM_CP_NULL);
MEM_CP_SET_POINTER (child_scope_header_p->next_header_cp, next_to_handle_list_p);
next_to_handle_list_p = child_scope_header_p;
}
bc_header_list_iter_p->next_header_cp = MEM_CP_NULL;
}
return true;
} /* bc_save_bytecode_data */
/**
* Register bytecode and supplementary data of a single scope from snapshot
*
* NOTE:
* If is_copy flag is set, bytecode is copied from snapshot, else bytecode is referenced directly
* from snapshot
*
* @return pointer to byte-code header, upon success,
* NULL - upon failure (i.e., in case snapshot format is not valid)
*/
static bytecode_data_header_t *
bc_load_bytecode_with_idx_map (const uint8_t *snapshot_data_p, /**< buffer with instructions array
* and idx to literals map from
* snapshot */
size_t snapshot_size, /**< remaining size of snapshot */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map of in-snapshot
* literal offsets
* to literal identifiers,
* created in literal
* storage */
uint32_t literals_num, /**< number of literals */
bool is_copy, /** flag, indicating whether the passed in-snapshot data
* should be copied to engine's memory (true),
* or it can be referenced until engine is stopped
* (i.e. until call to jerry_cleanup) */
uint32_t *out_bytecode_data_size) /**< out: size occupied by bytecode data
* in snapshot */
{
size_t buffer_offset = 0;
jerry_snapshot_bytecode_header_t bytecode_header;
if (!jrt_read_from_buffer_by_offset (snapshot_data_p,
snapshot_size,
&buffer_offset,
&bytecode_header))
{
return NULL;
}
*out_bytecode_data_size = bytecode_header.size;
buffer_offset += (JERRY_ALIGNUP (sizeof (jerry_snapshot_bytecode_header_t), MEM_ALIGNMENT)
- sizeof (jerry_snapshot_bytecode_header_t));
JERRY_ASSERT (bytecode_header.size <= snapshot_size);
/* Read uid->lit_cp hash table size */
const uint8_t *idx_to_lit_map_p = (snapshot_data_p
+ buffer_offset +
+ bytecode_header.instrs_size
+ bytecode_header.var_decls_count * sizeof (uint32_t));
size_t instructions_number = bytecode_header.instrs_size / sizeof (vm_instr_t);
size_t blocks_count = JERRY_ALIGNUP (instructions_number, BLOCK_SIZE) / BLOCK_SIZE;
uint32_t idx_num_total;
size_t idx_to_lit_map_offset = 0;
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
bytecode_header.idx_to_lit_map_size,
&idx_to_lit_map_offset,
&idx_num_total))
{
return NULL;
}
/* Alloc bytecode_header for runtime */
const size_t bytecode_alloc_size = JERRY_ALIGNUP (bytecode_header.instrs_size, MEM_ALIGNMENT);
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (idx_num_total, blocks_count);
const size_t declarations_area_size = JERRY_ALIGNUP (bytecode_header.func_scopes_count * sizeof (mem_cpointer_t)
+ bytecode_header.var_decls_count * sizeof (lit_cpointer_t),
MEM_ALIGNMENT);
const size_t header_and_tables_size = JERRY_ALIGNUP ((sizeof (bytecode_data_header_t)
+ hash_table_size
+ declarations_area_size),
MEM_ALIGNMENT);
const size_t alloc_size = header_and_tables_size + (is_copy ? bytecode_alloc_size : 0);
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (alloc_size, MEM_HEAP_ALLOC_LONG_TERM);
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
vm_instr_t *instrs_p;
vm_instr_t *snapshot_instrs_p = (vm_instr_t *) (snapshot_data_p + buffer_offset);
if (is_copy)
{
instrs_p = (vm_instr_t *) (buffer_p + header_and_tables_size);
memcpy (instrs_p, snapshot_instrs_p, bytecode_header.instrs_size);
}
else
{
instrs_p = snapshot_instrs_p;
}
buffer_offset += bytecode_header.instrs_size; /* buffer_offset is now offset of variable declarations */
/* Read uid->lit_cp hash table */
uint8_t *lit_id_hash_table_buffer_p = buffer_p + sizeof (bytecode_data_header_t);
if (!(lit_id_hash_table_load_from_snapshot (blocks_count,
idx_num_total,
idx_to_lit_map_p + idx_to_lit_map_offset,
bytecode_header.idx_to_lit_map_size - idx_to_lit_map_offset,
lit_map_p,
literals_num,
lit_id_hash_table_buffer_p,
hash_table_size)
&& (vm_instr_counter_t) instructions_number == instructions_number))
{
mem_heap_free_block (buffer_p);
return NULL;
}
/* Fill with NULLs child scopes declarations for this scope */
mem_cpointer_t *declarations_p = (mem_cpointer_t *) (buffer_p + sizeof (bytecode_data_header_t) + hash_table_size);
memset (declarations_p, 0, bytecode_header.func_scopes_count * sizeof (mem_cpointer_t));
/* Read variable declarations for this scope */
lit_cpointer_t *var_decls_p = (lit_cpointer_t *) (declarations_p + bytecode_header.func_scopes_count);
for (uint32_t i = 0; i < bytecode_header.var_decls_count; i++)
{
uint32_t lit_offset_from_snapshot;
if (!jrt_read_from_buffer_by_offset (snapshot_data_p,
buffer_offset + bytecode_header.var_decls_count * sizeof (uint32_t),
&buffer_offset,
&lit_offset_from_snapshot))
{
mem_heap_free_block (buffer_p);
return NULL;
}
/**
* TODO: implement binary search here
*/
lit_cpointer_t lit_cp = lit_cpointer_t::null_cp ();
uint32_t j;
for (j = 0; j < literals_num; j++)
{
if (lit_map_p[j].literal_offset == lit_offset_from_snapshot)
{
lit_cp.packed_value = lit_map_p[j].literal_id.packed_value;
break;
}
}
if (j == literals_num)
{
mem_heap_free_block (buffer_p);
return NULL;
}
var_decls_p[i] = lit_cp;
}
/* Fill bytecode_data_header */
bc_fill_bytecode_data_header (header_p,
(lit_id_hash_table *) lit_id_hash_table_buffer_p,
instrs_p,
declarations_p,
(uint16_t) bytecode_header.func_scopes_count,
(uint16_t) bytecode_header.var_decls_count,
bytecode_header.is_strict,
bytecode_header.is_ref_arguments_identifier,
bytecode_header.is_ref_eval_identifier,
bytecode_header.is_args_moved_to_regs,
bytecode_header.is_args_moved_to_regs,
bytecode_header.is_no_lex_env);
return header_p;
} /* bc_load_bytecode_with_idx_map */
/**
* Register bytecode and supplementary data of all scopes from snapshot
*
* NOTE:
* If is_copy flag is set, bytecode is copied from snapshot, else bytecode is referenced directly
* from snapshot
*
* @return pointer to byte-code header, upon success,
* NULL - upon failure (i.e., in case snapshot format is not valid)
*/
const bytecode_data_header_t *
bc_load_bytecode_data (const uint8_t *snapshot_data_p, /**< buffer with instructions array
* and idx to literals map from
* snapshot */
size_t snapshot_size, /**< remaining size of snapshot */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map of in-snapshot
* literal offsets
* to literal identifiers,
* created in literal
* storage */
uint32_t literals_num, /**< number of literals */
bool is_copy, /** flag, indicating whether the passed in-snapshot data
* should be copied to engine's memory (true),
* or it can be referenced until engine is stopped
* (i.e. until call to jerry_cleanup) */
uint32_t expected_scopes_num) /**< scopes number read from snapshot header */
{
uint32_t snapshot_offset = 0;
uint32_t out_bytecode_data_size = 0;
uint32_t scopes_num = 0;
bytecode_data_header_t *bc_header_p = bc_load_bytecode_with_idx_map (snapshot_data_p,
snapshot_size,
lit_map_p,
literals_num,
is_copy,
&out_bytecode_data_size);
scopes_num++;
snapshot_offset += out_bytecode_data_size;
JERRY_ASSERT (snapshot_offset < snapshot_size);
bytecode_data_header_t* next_to_handle_list_p = bc_header_p;
while (next_to_handle_list_p != NULL)
{
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t, next_to_handle_list_p->declarations_cp);
uint32_t child_scope_index = 0;
while (child_scope_index < next_to_handle_list_p->func_scopes_count
&& declarations_p[child_scope_index] != MEM_CP_NULL)
{
child_scope_index++;
}
if (child_scope_index == next_to_handle_list_p->func_scopes_count)
{
bytecode_data_header_t *bc_header_list_iter_p = MEM_CP_GET_POINTER (bytecode_data_header_t,
next_to_handle_list_p->next_header_cp);
next_to_handle_list_p->next_header_cp = MEM_CP_NULL;
next_to_handle_list_p = bc_header_list_iter_p;
if (next_to_handle_list_p == MEM_CP_NULL)
{
break;
}
else
{
continue;
}
}
JERRY_ASSERT (snapshot_size > snapshot_offset);
bytecode_data_header_t *next_header_p = bc_load_bytecode_with_idx_map (snapshot_data_p + snapshot_offset,
snapshot_size - snapshot_offset,
lit_map_p,
literals_num,
is_copy,
&out_bytecode_data_size);
scopes_num++;
snapshot_offset += out_bytecode_data_size;
JERRY_ASSERT (snapshot_offset <= snapshot_size);
MEM_CP_SET_NON_NULL_POINTER (declarations_p[child_scope_index], next_header_p);
if (next_header_p->func_scopes_count > 0)
{
JERRY_ASSERT (next_header_p->next_header_cp == MEM_CP_NULL);
MEM_CP_SET_POINTER (next_header_p->next_header_cp, next_to_handle_list_p);
next_to_handle_list_p = next_header_p;
}
}
if (expected_scopes_num != scopes_num)
{
return NULL;
}
MEM_CP_SET_POINTER (bc_header_p->next_header_cp, first_bytecode_header_p);
first_bytecode_header_p = bc_header_p;
return bc_header_p;
} /* bc_load_bytecode_data */
#endif /* JERRY_ENABLE_SNAPSHOT */

View File

@ -0,0 +1,109 @@
/* 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 BYTECODE_DATA_H
#define BYTECODE_DATA_H
#include "opcodes.h"
#include "mem-allocator.h"
#include "lit-id-hash-table.h"
#include "scopes-tree.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 'instrs' 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 32u
/**
* Header of byte-code memory region, containing byte-code array and literal identifiers hash table
*/
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) bytecode_data_header_t
{
vm_instr_t *instrs_p; /**< pointer to the bytecode */
vm_instr_counter_t instrs_count; /**< number of instructions in the byte-code array */
mem_cpointer_t lit_id_hash_cp; /**< pointer to literal identifiers hash table
* See also: lit_id_hash_table_init */
mem_cpointer_t declarations_cp; /**< function scopes and variable declarations inside current scope */
uint16_t func_scopes_count; /**< count of function scopes inside current scope */
uint16_t var_decls_count; /**< count of variable declrations inside current scope */
mem_cpointer_t next_header_cp; /**< pointer to next instructions data header */
uint8_t is_strict : 1; /**< code is strict mode code */
uint8_t is_ref_arguments_identifier : 1; /**< code doesn't reference 'arguments' identifier */
uint8_t is_ref_eval_identifier : 1; /**< code doesn't reference 'eval' identifier */
uint8_t is_vars_and_args_to_regs_possible : 1; /**< flag, indicating whether it is possible
* to safely perform var-to-reg
* optimization on the scope
*
* TODO: remove the flag when var-to-reg optimization
* would be moved from post-parse to dump stage */
uint8_t is_args_moved_to_regs : 1; /**< the function's arguments are moved to registers,
* so should be initialized in vm registers,
* and not in lexical environment */
uint8_t is_no_lex_env : 1; /**< no lex. env. is necessary for the scope */
} bytecode_data_header_t;
JERRY_STATIC_ASSERT (sizeof (bytecode_data_header_t) % MEM_ALIGNMENT == 0);
void bc_remove_bytecode_data (const bytecode_data_header_t *);
vm_instr_t bc_get_instr (const bytecode_data_header_t *,
vm_instr_counter_t);
void bc_print_instrs (const bytecode_data_header_t *);
bytecode_data_header_t *bc_dump_single_scope (scopes_tree);
void bc_register_root_bytecode_header (bytecode_data_header_t *);
void bc_finalize ();
lit_cpointer_t
bc_get_literal_cp_by_uid (uint8_t,
const bytecode_data_header_t *,
vm_instr_counter_t);
#ifdef JERRY_ENABLE_SNAPSHOT
/*
* Snapshot-related
*/
uint32_t
bc_find_lit_offset (lit_cpointer_t, const lit_mem_to_snapshot_id_map_entry_t *, uint32_t);
bool
bc_align_data_in_output_buffer (uint32_t *, uint8_t *, size_t, size_t *);
bool
bc_save_bytecode_data (uint8_t *, size_t, size_t *, const bytecode_data_header_t *,
const lit_mem_to_snapshot_id_map_entry_t *, uint32_t, uint32_t *);
const bytecode_data_header_t *
bc_load_bytecode_data (const uint8_t *, size_t,
const lit_mem_to_snapshot_id_map_entry_t *, uint32_t, bool, uint32_t);
#endif /* JERRY_ENABLE_SNAPSHOT */
#endif /* BYTECODE_DATA_H */

View File

@ -1,49 +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 BYTECODE_DATA_H
#define BYTECODE_DATA_H
#include "opcodes.h"
#include "mem-allocator.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 'instrs' 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 64u
/**
* Header of byte-code memory region, containing byte-code array and literal identifiers hash table
*/
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT))) bytecode_data_header_t
{
vm_instr_t *instrs_p; /**< pointer to the bytecode */
vm_instr_counter_t instrs_count; /**< number of instructions in the byte-code array */
mem_cpointer_t lit_id_hash_cp; /**< pointer to literal identifiers hash table
* See also: lit_id_hash_table_init */
mem_cpointer_t next_header_cp; /**< pointer to next instructions data header */
} bytecode_data_header_t;
#endif /* BYTECODE_DATA_H */

View File

@ -1,128 +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.
*/
#include "array-list.h"
#include "hash-table.h"
#include "jrt-libc-includes.h"
#include "jsp-mm.h"
typedef struct
{
uint16_t (*hash) (void *);
array_list *data;
uint16_t size;
uint8_t key_size;
uint8_t value_size;
} hash_table_int;
static hash_table_int *
extract_header (hash_table ht)
{
JERRY_ASSERT (ht != null_hash);
hash_table_int *hti = (hash_table_int *) ht;
return hti;
}
static uint8_t
bucket_size (hash_table_int *hti)
{
return (uint8_t) (hti->key_size + hti->value_size);
}
static array_list
get_list (hash_table_int *h, uint16_t i)
{
return h->data[i];
}
static void
set_list (hash_table_int *h, uint16_t i, array_list al)
{
h->data[i] = al;
}
void
hash_table_insert (hash_table ht, void *key, void *value)
{
hash_table_int *hti = extract_header (ht);
uint16_t index = hti->hash (key);
JERRY_ASSERT (index < hti->size);
array_list list = get_list (hti, index);
if (list == null_list)
{
list = array_list_init (bucket_size (hti));
}
uint8_t *bucket = (uint8_t *) jsp_mm_alloc (bucket_size (hti));
memcpy (bucket, key, hti->key_size);
memcpy (bucket + hti->key_size, value, hti->value_size);
list = array_list_append (list, bucket);
hti->data[index] = list;
jsp_mm_free (bucket);
}
void *
hash_table_lookup (hash_table ht, void *key)
{
JERRY_ASSERT (key != NULL);
hash_table_int *h = extract_header (ht);
uint16_t index = h->hash (key);
array_list al = get_list (h, index);
if (al == null_list)
{
return NULL;
}
for (uint16_t i = 0; i < array_list_len (al); i++)
{
uint8_t *bucket = (uint8_t*) array_list_element (al, i);
JERRY_ASSERT (bucket != NULL);
if (!memcmp (bucket, key, h->key_size))
{
return bucket + h->key_size;
}
}
return NULL;
}
hash_table
hash_table_init (uint8_t key_size, uint8_t value_size, uint16_t size,
uint16_t (*hash) (void *))
{
hash_table_int *res = (hash_table_int *) jsp_mm_alloc (sizeof (hash_table_int));
memset (res, 0, sizeof (hash_table_int));
res->key_size = key_size;
res->value_size = value_size;
res->size = size;
res->data = (array_list *) jsp_mm_alloc (size * sizeof (array_list));
memset (res->data, 0, size * sizeof (array_list));
res->hash = hash;
return res;
}
void
hash_table_free (hash_table ht)
{
hash_table_int *h = extract_header (ht);
for (uint16_t i = 0; i < h->size; i++)
{
array_list al = get_list (h, i);
if (al != null_list)
{
array_list_free (al);
set_list (h, i, null_list);
}
}
jsp_mm_free (h->data);
jsp_mm_free (h);
}

View File

@ -1,38 +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.
*/
/**
This file contains functions to create and manipulate hash-tables.
Before using the hash initialize it by calling hash_table_init function.
The function takes pointer to hash function as the last parameter.
URGENT: the result of the hash function must not be greater than size of the hash table.
To insert a key-value pair, use hash_table_insert function.
To lookup a value by the key, use hash_table_lookup function.
After using the hash, delete it by calling hash_table_free function.
*/
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#include "mem-heap.h"
typedef void *hash_table;
#define null_hash NULL
hash_table hash_table_init (uint8_t, uint8_t, uint16_t, uint16_t (*hash) (void *));
void hash_table_free (hash_table);
void hash_table_insert (hash_table, void *, void *);
void *hash_table_lookup (hash_table, void *);
#endif /* HASH_TABLE_H */

View File

@ -291,7 +291,6 @@ linked_list_remove_element (linked_list list, /**< linked list's identifier */
}
uint8_t *next_elem_iter_p = linked_list_switch_to_next_elem (header_p, &list_chunk_iter_p, element_iter_p);
JERRY_ASSERT (next_elem_iter_p != NULL);
linked_list_chunk_header *chunk_prev_to_chunk_with_last_elem_p = list_chunk_iter_p;

View File

@ -84,11 +84,12 @@ lit_id_hash_table_free (lit_id_hash_table *table_p) /**< table's header */
} /* lit_id_hash_table_free */
/**
* Register pair in the hash table
* Register literal in the hash table
*
* @return corresponding idx
*/
void
vm_idx_t
lit_id_hash_table_insert (lit_id_hash_table *table_p, /**< table's header */
vm_idx_t uid, /**< value of byte-code instruction's argument */
vm_instr_counter_t oc, /**< instruction counter of the instruction */
lit_cpointer_t lit_cp) /**< literal identifier */
{
@ -101,8 +102,31 @@ lit_id_hash_table_insert (lit_id_hash_table *table_p, /**< table's header */
table_p->buckets[block_id] = table_p->raw_buckets + table_p->current_bucket_pos;
}
table_p->buckets[block_id][uid] = lit_cp;
table_p->current_bucket_pos++;
lit_cpointer_t *raw_bucket_iter_p = table_p->raw_buckets + table_p->current_bucket_pos;
JERRY_ASSERT (raw_bucket_iter_p >= table_p->buckets[block_id]);
ssize_t bucket_size = (raw_bucket_iter_p - table_p->buckets[block_id]);
int32_t index;
for (index = 0; index < bucket_size; index++)
{
if (table_p->buckets[block_id][index].packed_value == lit_cp.packed_value)
{
break;
}
}
if (index == bucket_size)
{
JERRY_ASSERT ((uint8_t *) (table_p->raw_buckets + table_p->current_bucket_pos) < (uint8_t *) (table_p->buckets));
table_p->buckets[block_id][index] = lit_cp;
table_p->current_bucket_pos++;
}
JERRY_ASSERT (index <= VM_IDX_LITERAL_LAST);
return (vm_idx_t) index;
} /* lit_id_hash_table_insert */
/**
@ -194,17 +218,8 @@ lit_id_hash_table_dump_for_snapshot (uint8_t *buffer_p, /**< buffer to dump to *
{
lit_cpointer_t lit_cp = table_p->buckets[block_index][block_idx_pair_index];
uint32_t lit_index;
for (lit_index = 0; lit_index < literals_num; lit_index++)
{
if (lit_map_p[lit_index].literal_id.packed_value == lit_cp.packed_value)
{
break;
}
}
JERRY_ASSERT (lit_index < literals_num);
uint32_t offset = bc_find_lit_offset (lit_cp, lit_map_p, literals_num);
uint32_t offset = lit_map_p[lit_index].literal_offset;
if (!jrt_write_to_buffer_by_offset (buffer_p, buffer_size, in_out_buffer_offset_p, offset))
{
return 0;

View File

@ -31,7 +31,7 @@ typedef struct
lit_id_hash_table *lit_id_hash_table_init (uint8_t *, size_t, size_t, size_t);
size_t lit_id_hash_table_get_size_for_table (size_t, size_t);
void lit_id_hash_table_free (lit_id_hash_table *);
void lit_id_hash_table_insert (lit_id_hash_table *, vm_idx_t, vm_instr_counter_t, lit_cpointer_t);
vm_idx_t lit_id_hash_table_insert (lit_id_hash_table *,vm_instr_counter_t, lit_cpointer_t);
lit_cpointer_t lit_id_hash_table_lookup (lit_id_hash_table *, vm_idx_t, vm_instr_counter_t);
uint32_t lit_id_hash_table_dump_for_snapshot (uint8_t *, size_t, size_t *, lit_id_hash_table *,
const lit_mem_to_snapshot_id_map_entry_t *, uint32_t, vm_instr_counter_t);

View File

@ -116,7 +116,7 @@ jsp_early_error_start_checking_of_prop_names (void)
void
jsp_early_error_add_prop_name (jsp_operand_t op, prop_type pt)
{
JERRY_ASSERT (op.is_literal_operand ());
JERRY_ASSERT (op.is_string_lit_operand ());
STACK_PUSH (props, create_prop_literal (lit_get_literal_by_cp (op.get_literal ()), pt));
}
@ -135,24 +135,18 @@ jsp_early_error_check_for_duplication_of_prop_names (bool is_strict, locus loc _
i++)
{
const prop_literal previous = STACK_ELEMENT (props, i);
if (previous.type == VARG)
{
continue;
}
JERRY_ASSERT (previous.type == PROP_DATA
|| previous.type == PROP_GET
|| previous.type == PROP_SET);
for (size_t j = STACK_TOP (size_t_stack); j < i; j = j + 1)
{
/*4*/
const prop_literal current = STACK_ELEMENT (props, j);
if (current.type == VARG)
{
continue;
}
JERRY_ASSERT (current.type == PROP_DATA
|| current.type == PROP_GET
|| current.type == PROP_SET);
if (lit_literal_equal (previous.lit, current.lit))
{
/*a*/
@ -195,19 +189,8 @@ jsp_early_error_check_for_duplication_of_prop_names (bool is_strict, locus loc _
}
void
jsp_early_error_start_checking_of_vargs (void)
{
STACK_PUSH (size_t_stack, STACK_SIZE (props));
}
void jsp_early_error_add_varg (jsp_operand_t op)
{
JERRY_ASSERT (op.is_literal_operand ());
STACK_PUSH (props, create_prop_literal (lit_get_literal_by_cp (op.get_literal ()), VARG));
}
static void
emit_error_on_eval_and_arguments (literal_t lit, locus loc __attr_unused___)
jsp_early_error_emit_error_on_eval_and_arguments (literal_t lit, /**< literal to check */
locus loc) /**< location of the literal in source code */
{
if (lit_literal_equal_type_utf8 (lit,
lit_get_magic_string_utf8 (LIT_MAGIC_STRING_ARGUMENTS),
@ -223,49 +206,26 @@ emit_error_on_eval_and_arguments (literal_t lit, locus loc __attr_unused___)
void
jsp_early_error_check_for_eval_and_arguments_in_strict_mode (jsp_operand_t op, bool is_strict, locus loc)
{
if (is_strict
&& op.is_literal_operand ())
if (is_strict)
{
emit_error_on_eval_and_arguments (lit_get_literal_by_cp (op.get_literal ()), loc);
}
}
lit_cpointer_t lit_cp;
/* 13.1, 15.3.2 */
void
jsp_early_error_check_for_syntax_errors_in_formal_param_list (bool is_strict, locus loc)
{
if (is_strict
&& STACK_SIZE (props) - STACK_TOP (size_t_stack) >= 1)
{
for (size_t i = STACK_TOP (size_t_stack); i < STACK_SIZE (props); i++)
if (op.is_string_lit_operand ()
|| op.is_number_lit_operand ())
{
JERRY_ASSERT (STACK_ELEMENT (props, i).type == VARG);
literal_t previous = STACK_ELEMENT (props, i).lit;
JERRY_ASSERT (previous->get_type () == LIT_STR_T
|| previous->get_type () == LIT_MAGIC_STR_T
|| previous->get_type () == LIT_MAGIC_STR_EX_T);
emit_error_on_eval_and_arguments (previous, loc);
for (size_t j = i + 1; j < STACK_SIZE (props); j++)
{
JERRY_ASSERT (STACK_ELEMENT (props, j).type == VARG);
literal_t current = STACK_ELEMENT (props, j).lit;
JERRY_ASSERT (current->get_type () == LIT_STR_T
|| current->get_type () == LIT_MAGIC_STR_T
|| current->get_type () == LIT_MAGIC_STR_EX_T);
if (lit_literal_equal_type (previous, current))
{
PARSE_ERROR_VARG (JSP_EARLY_ERROR_SYNTAX,
"Duplication of literal '%s' in FormalParameterList is not allowed in strict mode",
loc, lit_literal_to_str_internal_buf (previous));
}
}
lit_cp = op.get_literal ();
}
else if (op.is_identifier_operand ())
{
lit_cp = op.get_identifier_name ();
}
else
{
return;
}
}
STACK_DROP (props, (size_t) (STACK_SIZE (props) - STACK_TOP (size_t_stack)));
STACK_DROP (size_t_stack, 1);
jsp_early_error_emit_error_on_eval_and_arguments (lit_get_literal_by_cp (lit_cp), loc);
}
}
void

View File

@ -47,10 +47,10 @@
} while (0)
#else /* JERRY_NDEBUG */
#define PARSE_ERROR(type, MESSAGE, LOCUS) do { \
jsp_early_error_raise_error (type); \
locus __attr_unused___ unused_value = LOCUS; jsp_early_error_raise_error (type); \
} while (0)
#define PARSE_ERROR_VARG(type, MESSAGE, LOCUS, ...) do { \
jsp_early_error_raise_error (type); \
locus __attr_unused___ unused_value = LOCUS; jsp_early_error_raise_error (type); \
} while (0)
#endif /* JERRY_NDEBUG */
@ -58,8 +58,7 @@ typedef enum __attr_packed___
{
PROP_DATA,
PROP_SET,
PROP_GET,
VARG
PROP_GET
} prop_type;
/**
@ -79,10 +78,8 @@ void jsp_early_error_start_checking_of_prop_names (void);
void jsp_early_error_add_prop_name (jsp_operand_t, prop_type);
void jsp_early_error_check_for_duplication_of_prop_names (bool, locus);
void jsp_early_error_start_checking_of_vargs (void);
void jsp_early_error_add_varg (jsp_operand_t);
void jsp_early_error_emit_error_on_eval_and_arguments (literal_t, locus);
void jsp_early_error_check_for_eval_and_arguments_in_strict_mode (jsp_operand_t, bool, locus);
void jsp_early_error_check_for_syntax_errors_in_formal_param_list (bool, locus);
void jsp_early_error_check_delete (bool, locus);

View File

@ -0,0 +1,122 @@
/* 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 JSP_INTERNAL_H
#define JSP_INTERNAL_H
#include "scopes-tree.h"
/**
* Parse stage
*/
typedef enum
{
PREPARSE, /**< preparse stage */
DUMP /**< dump stage */
} jsp_parse_mode_t;
/**
* Size of the temporary literal set
*/
#define SCOPE_TMP_LIT_SET_SIZE 32
/**
* Parser context
*/
typedef struct
{
struct jsp_state_t *state_stack_p; /**< parser stack */
jsp_parse_mode_t mode; /**< parse stage */
scope_type_t scope_type; /**< type of currently parsed scope */
uint16_t processed_child_scopes_counter; /**< number of processed
* child scopes of the
* current scope */
union
{
/**
* Preparse stage information
*/
struct
{
/**
* Currently parsed scope
*/
scopes_tree current_scope_p;
/**
* Container of the temporary literal set
*
* Temporary literal set is used for estimation of number of unique literals
* in a byte-code instructions block (BLOCK_SIZE). The calculated number
* is always equal or larger than actual number of unique literals.
*
* The set is emptied upon:
* - reaching a bytecode block border;
* - changing scope, to which instructions are dumped.
*
* Emptying the set in second case is necessary, as the set should contain
* unique literals of a bytecode block, and, upon switching to another scope,
* current bytecode block is also switched, correspondingly. However, this
* could only lead to overestimation of unique literals number, relatively
* to the actual number.
*/
lit_cpointer_t tmp_lit_set[SCOPE_TMP_LIT_SET_SIZE];
/**
* Number of items in the temporary literal set
*/
uint8_t tmp_lit_set_num;
} preparse_stage;
/**
* Dump stage information
*/
struct
{
/**
* Current scope's byte-code header
*/
bytecode_data_header_t *current_bc_header_p;
/**
* Pointer to the scope that would be parsed next
*/
scopes_tree next_scope_to_dump_p;
} dump_stage;
} u;
} jsp_ctx_t;
void jsp_init_ctx (jsp_ctx_t *, scope_type_t);
bool jsp_is_dump_mode (jsp_ctx_t *);
void jsp_switch_to_dump_mode (jsp_ctx_t *, scopes_tree);
bool jsp_is_strict_mode (jsp_ctx_t *);
void jsp_set_strict_mode (jsp_ctx_t *);
scope_type_t jsp_get_scope_type (jsp_ctx_t *);
void jsp_set_scope_type (jsp_ctx_t *, scope_type_t);
uint16_t jsp_get_processed_child_scopes_counter (jsp_ctx_t *);
void jsp_set_processed_child_scopes_counter (jsp_ctx_t *, uint16_t);
uint16_t jsp_get_and_inc_processed_child_scopes_counter (jsp_ctx_t *);
scopes_tree jsp_get_next_scopes_tree_node_to_dump (jsp_ctx_t *);
scopes_tree jsp_get_current_scopes_tree_node (jsp_ctx_t *);
void jsp_set_current_scopes_tree_node (jsp_ctx_t *, scopes_tree);
bytecode_data_header_t *jsp_get_current_bytecode_header (jsp_ctx_t *);
void jsp_set_current_bytecode_header (jsp_ctx_t *, bytecode_data_header_t *);
void jsp_empty_tmp_literal_set (jsp_ctx_t *);
void jsp_account_next_bytecode_to_literal_reference (jsp_ctx_t *, lit_cpointer_t);
#endif /* !JSP_INTERNAL_H */

View File

@ -1,325 +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.
*/
#include "jsp-label.h"
#include "lexer.h"
#include "opcodes-dumper.h"
/** \addtogroup jsparser ECMAScript parser
* @{
*
* \addtogroup labels Jump labels
* @{
*/
/**
* Stack, containing current label set
*/
jsp_label_t *label_set_p = NULL;
/**
* Initialize jumps labels mechanism
*/
void
jsp_label_init (void)
{
JERRY_ASSERT (label_set_p == NULL);
} /* jsp_label_init */
/**
* Finalize jumps labels mechanism
*/
void
jsp_label_finalize (void)
{
JERRY_ASSERT (label_set_p == NULL);
} /* jsp_label_finalize */
/**
* Remove all labels
*
* Note:
* should be used only upon a SyntaxError is raised
*/
void
jsp_label_remove_all_labels (void)
{
label_set_p = NULL;
} /* jsp_label_remove_all_labels */
/**
* Add label to the current label set
*/
void
jsp_label_push (jsp_label_t *out_label_p, /**< out: place where label structure
* should be initialized and
* linked into label set stack */
jsp_label_type_flag_t type_mask, /**< label's type mask */
token id) /**< identifier of the label (TOK_NAME)
* if mask includes JSP_LABEL_TYPE_NAMED,
* or empty token - otherwise */
{
JERRY_ASSERT (out_label_p != NULL);
if (type_mask & JSP_LABEL_TYPE_NAMED)
{
JERRY_ASSERT (id.type == TOK_NAME);
JERRY_ASSERT (jsp_label_find (JSP_LABEL_TYPE_NAMED, id, NULL) == NULL);
}
else
{
JERRY_ASSERT (id.type == TOK_EMPTY);
}
out_label_p->type_mask = type_mask;
out_label_p->id = id;
out_label_p->continue_tgt_oc = MAX_OPCODES;
out_label_p->breaks_list_oc = MAX_OPCODES;
out_label_p->breaks_number = 0;
out_label_p->continues_list_oc = MAX_OPCODES;
out_label_p->continues_number = 0;
out_label_p->next_label_p = label_set_p;
out_label_p->is_nested_jumpable_border = false;
label_set_p = out_label_p;
} /* jsp_label_push */
/**
* Rewrite jumps to the label, if there any,
* and remove it from the current label set
*
* @return the label should be on top of label set stack
*/
void
jsp_label_rewrite_jumps_and_pop (jsp_label_t *label_p, /**< label to remove (should be on top of stack) */
vm_instr_counter_t break_tgt_oc) /**< target instruction counter
* for breaks on the label */
{
JERRY_ASSERT (label_p != NULL);
JERRY_ASSERT (break_tgt_oc != MAX_OPCODES);
JERRY_ASSERT (label_set_p == label_p);
/* Iterating jumps list, rewriting them */
while (label_p->breaks_number--)
{
JERRY_ASSERT (label_p->breaks_list_oc != MAX_OPCODES);
label_p->breaks_list_oc = rewrite_simple_or_nested_jump_and_get_next (label_p->breaks_list_oc,
break_tgt_oc);
}
while (label_p->continues_number--)
{
JERRY_ASSERT (label_p->continue_tgt_oc != MAX_OPCODES);
JERRY_ASSERT (label_p->continues_list_oc != MAX_OPCODES);
label_p->continues_list_oc = rewrite_simple_or_nested_jump_and_get_next (label_p->continues_list_oc,
label_p->continue_tgt_oc);
}
label_set_p = label_set_p->next_label_p;
} /* jsp_label_rewrite_jumps_and_pop */
/**
* Find label with specified identifier
*
* @return if found, pointer to label descriptor,
* otherwise - NULL.
*/
jsp_label_t*
jsp_label_find (jsp_label_type_flag_t type_mask, /**< types to search for */
token id, /**< identifier of the label (TOK_NAME)
* if mask equals to JSP_LABEL_TYPE_NAMED,
* or empty token - otherwise
* (if so, mask should not include JSP_LABEL_TYPE_NAMED) */
bool *out_is_simply_jumpable_p) /**< out: is the label currently
* accessible with a simple jump */
{
bool is_search_named = (type_mask == JSP_LABEL_TYPE_NAMED);
if (is_search_named)
{
JERRY_ASSERT (id.type == TOK_NAME);
}
else
{
JERRY_ASSERT (!(type_mask & JSP_LABEL_TYPE_NAMED));
JERRY_ASSERT (id.type == TOK_EMPTY);
}
bool is_simply_jumpable = true;
jsp_label_t *ret_label_p = NULL;
for (jsp_label_t *label_iter_p = label_set_p;
label_iter_p != NULL;
label_iter_p = label_iter_p->next_label_p)
{
if (label_iter_p->is_nested_jumpable_border)
{
is_simply_jumpable = false;
}
bool is_named_label = (label_iter_p->type_mask & JSP_LABEL_TYPE_NAMED);
if ((is_search_named
&& is_named_label
&& lexer_are_tokens_with_same_identifier (label_iter_p->id, id))
|| (!is_search_named
&& (type_mask & label_iter_p->type_mask)))
{
ret_label_p = label_iter_p;
break;
}
}
if (out_is_simply_jumpable_p != NULL)
{
*out_is_simply_jumpable_p = is_simply_jumpable;
}
return ret_label_p;
} /* jsp_label_find */
/**
* Dump jump and register it in the specified label to be rewritten later (see also: jsp_label_rewrite_jumps_and_pop)
*
* Warning:
* The dumped instruction should not be modified before it is rewritten, as its idx fields are used
* to link jump instructions related to the label into singly linked list.
*/
void
jsp_label_add_jump (jsp_label_t *label_p, /**< label to register jump for */
bool is_simply_jumpable, /**< is the label currently
* accessible with a simple jump */
bool is_break) /**< type of jump - 'break' (true) or 'continue' (false) */
{
JERRY_ASSERT (label_p != NULL);
if (is_break)
{
label_p->breaks_list_oc = dump_simple_or_nested_jump_for_rewrite (is_simply_jumpable,
label_p->breaks_list_oc);
label_p->breaks_number++;
}
else
{
label_p->continues_list_oc = dump_simple_or_nested_jump_for_rewrite (is_simply_jumpable,
label_p->continues_list_oc);
label_p->continues_number++;
}
} /* jsp_label_add_jump */
/**
* Setup target for 'continue' jumps,
* associated with the labels, from innermost
* to the specified label.
*/
void
jsp_label_setup_continue_target (jsp_label_t *outermost_label_p, /**< the outermost label to setup target for */
vm_instr_counter_t tgt_oc) /**< target */
{
/* There are no labels that could not be targeted with 'break' jumps */
JERRY_ASSERT (tgt_oc != MAX_OPCODES);
JERRY_ASSERT (outermost_label_p != NULL);
for (jsp_label_t *label_iter_p = label_set_p;
label_iter_p != outermost_label_p->next_label_p;
label_iter_p = label_iter_p->next_label_p)
{
JERRY_ASSERT (label_iter_p != NULL);
JERRY_ASSERT (label_iter_p->continue_tgt_oc == MAX_OPCODES);
label_iter_p->continue_tgt_oc = tgt_oc;
}
} /* jsp_label_setup_continue_target */
/**
* Add nested jumpable border at current label, if there is any.
*
* @return true - if the border is added (in the case, it should be removed
* using jsp_label_remove_nested_jumpable_border, when parse of
* the corresponding statement would be finished),
* false - otherwise, new border is not raised, because there are no labels,
* or current label already contains a border.
*/
bool
jsp_label_raise_nested_jumpable_border (void)
{
bool is_any_label = (label_set_p != NULL);
if (is_any_label)
{
if (!label_set_p->is_nested_jumpable_border)
{
label_set_p->is_nested_jumpable_border = true;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
} /* jsp_label_raise_nested_jumpable_border */
/**
* Remove nested jumpable border from current label
*/
void
jsp_label_remove_nested_jumpable_border (void)
{
JERRY_ASSERT (label_set_p != NULL && label_set_p->is_nested_jumpable_border);
label_set_p->is_nested_jumpable_border = false;
} /* jsp_label_remove_nested_jumpable_border */
/**
* Mask current label set to restore it later, and start new label set
*
* @return pointer to masked label set's list of labels
*/
jsp_label_t*
jsp_label_mask_set (void)
{
jsp_label_t *ret_p = label_set_p;
label_set_p = NULL;
return ret_p;
} /* jsp_label_mask_set */
/**
* Restore previously masked label set
*
* Note:
* current label set should be empty
*/
void
jsp_label_restore_set (jsp_label_t *masked_label_set_list_p) /**< list of labels of
* a masked label set */
{
JERRY_ASSERT (label_set_p == NULL);
label_set_p = masked_label_set_list_p;
} /* jsp_label_restore_set */
/**
* @}
* @}
*/

View File

@ -1,91 +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 JSP_LABEL_H
#define JSP_LABEL_H
#include "lexer.h"
#include "opcodes.h"
/** \addtogroup jsparser ECMAScript parser
* @{
*
* \addtogroup labels Jump labels
* @{
*/
/**
* Label types
*/
typedef enum
{
JSP_LABEL_TYPE_NAMED = (1 << 0), /**< label for breaks and continues with identifiers */
JSP_LABEL_TYPE_UNNAMED_BREAKS = (1 << 1), /**< label for breaks without identifiers */
JSP_LABEL_TYPE_UNNAMED_CONTINUES = (1 << 2) /**< label for continues without identifiers */
} jsp_label_type_flag_t;
/**
* Descriptor of a jump label (See also: ECMA-262 v5, 12.12, Labelled statements)
*
* Note:
* Jump instructions with target identified by some specific label,
* are linked into singly-linked list.
*
* Pointer to a next element of the list is represented with instruction counter.
* stored in instructions linked into the list.
*
*/
typedef struct jsp_label_t
{
jsp_label_type_flag_t type_mask; /**< label type mask */
token id; /**< label name (TOK_NAME), if type is LABEL_NAMED */
vm_instr_counter_t continue_tgt_oc; /**< target instruction counter for continues on the label */
vm_instr_counter_t breaks_list_oc; /**< instruction counter of first 'break' instruction in the list
* of instructions with the target identified by the label */
vm_instr_counter_t breaks_number; /**< number of 'break' instructions in the list */
vm_instr_counter_t continues_list_oc; /**< instruction counter of first 'continue' instruction in the list
* of instructions with the target identified by the label */
vm_instr_counter_t continues_number; /**< number of 'continue' instructions in the list */
jsp_label_t *next_label_p; /**< next label in current label set stack */
bool is_nested_jumpable_border : 1; /**< flag, indicating that this and outer labels
* are not currently accessible with simple jumps,
* and so should be targetted with nested jumps only */
} jsp_label_t;
extern void jsp_label_init (void);
extern void jsp_label_finalize (void);
extern void jsp_label_remove_all_labels (void);
extern void jsp_label_push (jsp_label_t *, jsp_label_type_flag_t, token);
extern void jsp_label_rewrite_jumps_and_pop (jsp_label_t *, vm_instr_counter_t);
extern jsp_label_t *jsp_label_find (jsp_label_type_flag_t, token, bool *);
extern void jsp_label_add_jump (jsp_label_t *, bool, bool);
extern void jsp_label_setup_continue_target (jsp_label_t *, vm_instr_counter_t);
extern bool jsp_label_raise_nested_jumpable_border (void);
extern void jsp_label_remove_nested_jumpable_border (void);
extern jsp_label_t *jsp_label_mask_set (void);
extern void jsp_label_restore_set (jsp_label_t *);
/**
* @}
* @}
*/
#endif /* !JSP_LABEL_H */

View File

@ -28,12 +28,19 @@
/**
* Header of a managed block, allocated by parser
*/
typedef struct __attribute__ ((alignment (MEM_ALIGNMENT)))
typedef struct
{
mem_cpointer_t prev_block_cp; /**< previous managed block */
mem_cpointer_t next_block_cp; /**< next managed block */
uint32_t padding; /**< padding for alignment */
} jsp_mm_header_t;
/**
* Check that alignment of jsp_mm_header_t is MEM_ALIGNMENT
*/
JERRY_STATIC_ASSERT ((sizeof (jsp_mm_header_t) % MEM_ALIGNMENT) == 0);
/**
* List used for tracking memory blocks
*/

View File

@ -23,9 +23,9 @@
#include "lit-strings.h"
#include "jsp-early-error.h"
static token saved_token, prev_token, sent_token, empty_token, prev_non_lf_token;
static token saved_token, prev_token, sent_token, empty_token;
static bool allow_dump_lines = false, strict_mode;
static bool allow_dump_lines = false;
static size_t buffer_size = 0;
/*
@ -47,7 +47,7 @@ static lit_utf8_iterator_t src_iter;
static bool
is_empty (token tok)
{
return tok.type == TOK_EMPTY;
return lexer_get_token_type (tok) == TOK_EMPTY;
}
static locus
@ -119,7 +119,7 @@ dump_current_line (void)
} /* dump_current_line */
static token
create_token_from_lit (token_type type, literal_t lit)
create_token_from_lit (jsp_token_type_t type, literal_t lit)
{
token ret;
@ -136,8 +136,8 @@ create_token_from_lit (token_type type, literal_t lit)
* @return token descriptor
*/
static token
create_token (token_type type, /**< type of token */
uint16_t uid) /**< uid of token */
create_token (jsp_token_type_t type, /**< type of token */
uint16_t uid) /**< uid of token */
{
token ret;
@ -154,7 +154,7 @@ create_token (token_type type, /**< type of token */
* @return token descriptor
*/
static token
lexer_create_token_for_charset (token_type tt, /**< token type */
lexer_create_token_for_charset (jsp_token_type_t tt, /**< token type */
const lit_utf8_byte_t *charset_p, /**< charset buffer */
lit_utf8_size_t size) /**< size of the charset */
{
@ -509,7 +509,7 @@ lexer_transform_escape_sequences (const jerry_api_char_t *source_str_p, /**< str
* @return token descriptor
*/
static token
lexer_create_token_for_charset_transform_escape_sequences (token_type tt, /**< token type */
lexer_create_token_for_charset_transform_escape_sequences (jsp_token_type_t tt, /**< token type */
const lit_utf8_byte_t *charset_p, /**< charset buffer */
lit_utf8_size_t size) /**< size of the charset */
{
@ -531,70 +531,71 @@ lexer_create_token_for_charset_transform_escape_sequences (token_type tt, /**< t
/**
* Try to decode specified string as ReservedWord (ECMA-262 v5, 7.6.1)
*
* @return TOK_KEYWORD - for Keyword or FutureReservedWord,
* @return TOK_KW_* - for Keyword or FutureReservedWord,
* TOK_NULL - for NullLiteral,
* TOK_BOOL - for BooleanLiteral,
* TOK_EMPTY - for other tokens.
*/
static token
lexer_parse_reserved_word (const lit_utf8_byte_t *str_p, /**< characters buffer */
lit_utf8_size_t str_size) /**< string's length */
lit_utf8_size_t str_size, /**< string's length */
bool is_strict) /**< flag, indicating whether current code is in strict mode code */
{
typedef struct
{
const char *keyword_p;
keyword keyword_id;
jsp_token_type_t keyword_id;
} kw_descr_t;
const kw_descr_t keywords[] =
{
#define KW_DESCR(literal, keyword_id) { literal, keyword_id }
KW_DESCR ("break", KW_BREAK),
KW_DESCR ("case", KW_CASE),
KW_DESCR ("catch", KW_CATCH),
KW_DESCR ("class", KW_CLASS),
KW_DESCR ("const", KW_CONST),
KW_DESCR ("continue", KW_CONTINUE),
KW_DESCR ("debugger", KW_DEBUGGER),
KW_DESCR ("default", KW_DEFAULT),
KW_DESCR ("delete", KW_DELETE),
KW_DESCR ("do", KW_DO),
KW_DESCR ("else", KW_ELSE),
KW_DESCR ("enum", KW_ENUM),
KW_DESCR ("export", KW_EXPORT),
KW_DESCR ("extends", KW_EXTENDS),
KW_DESCR ("finally", KW_FINALLY),
KW_DESCR ("for", KW_FOR),
KW_DESCR ("function", KW_FUNCTION),
KW_DESCR ("if", KW_IF),
KW_DESCR ("in", KW_IN),
KW_DESCR ("instanceof", KW_INSTANCEOF),
KW_DESCR ("interface", KW_INTERFACE),
KW_DESCR ("import", KW_IMPORT),
KW_DESCR ("implements", KW_IMPLEMENTS),
KW_DESCR ("let", KW_LET),
KW_DESCR ("new", KW_NEW),
KW_DESCR ("package", KW_PACKAGE),
KW_DESCR ("private", KW_PRIVATE),
KW_DESCR ("protected", KW_PROTECTED),
KW_DESCR ("public", KW_PUBLIC),
KW_DESCR ("return", KW_RETURN),
KW_DESCR ("static", KW_STATIC),
KW_DESCR ("super", KW_SUPER),
KW_DESCR ("switch", KW_SWITCH),
KW_DESCR ("this", KW_THIS),
KW_DESCR ("throw", KW_THROW),
KW_DESCR ("try", KW_TRY),
KW_DESCR ("typeof", KW_TYPEOF),
KW_DESCR ("var", KW_VAR),
KW_DESCR ("void", KW_VOID),
KW_DESCR ("while", KW_WHILE),
KW_DESCR ("with", KW_WITH),
KW_DESCR ("yield", KW_YIELD)
KW_DESCR ("break", TOK_KW_BREAK),
KW_DESCR ("case", TOK_KW_CASE),
KW_DESCR ("catch", TOK_KW_CATCH),
KW_DESCR ("class", TOK_KW_CLASS),
KW_DESCR ("const", TOK_KW_CONST),
KW_DESCR ("continue", TOK_KW_CONTINUE),
KW_DESCR ("debugger", TOK_KW_DEBUGGER),
KW_DESCR ("default", TOK_KW_DEFAULT),
KW_DESCR ("delete", TOK_KW_DELETE),
KW_DESCR ("do", TOK_KW_DO),
KW_DESCR ("else", TOK_KW_ELSE),
KW_DESCR ("enum", TOK_KW_ENUM),
KW_DESCR ("export", TOK_KW_EXPORT),
KW_DESCR ("extends", TOK_KW_EXTENDS),
KW_DESCR ("finally", TOK_KW_FINALLY),
KW_DESCR ("for", TOK_KW_FOR),
KW_DESCR ("function", TOK_KW_FUNCTION),
KW_DESCR ("if", TOK_KW_IF),
KW_DESCR ("in", TOK_KW_IN),
KW_DESCR ("instanceof", TOK_KW_INSTANCEOF),
KW_DESCR ("interface", TOK_KW_INTERFACE),
KW_DESCR ("import", TOK_KW_IMPORT),
KW_DESCR ("implements", TOK_KW_IMPLEMENTS),
KW_DESCR ("let", TOK_KW_LET),
KW_DESCR ("new", TOK_KW_NEW),
KW_DESCR ("package", TOK_KW_PACKAGE),
KW_DESCR ("private", TOK_KW_PRIVATE),
KW_DESCR ("protected", TOK_KW_PROTECTED),
KW_DESCR ("public", TOK_KW_PUBLIC),
KW_DESCR ("return", TOK_KW_RETURN),
KW_DESCR ("static", TOK_KW_STATIC),
KW_DESCR ("super", TOK_KW_SUPER),
KW_DESCR ("switch", TOK_KW_SWITCH),
KW_DESCR ("this", TOK_KW_THIS),
KW_DESCR ("throw", TOK_KW_THROW),
KW_DESCR ("try", TOK_KW_TRY),
KW_DESCR ("typeof", TOK_KW_TYPEOF),
KW_DESCR ("var", TOK_KW_VAR),
KW_DESCR ("void", TOK_KW_VOID),
KW_DESCR ("while", TOK_KW_WHILE),
KW_DESCR ("with", TOK_KW_WITH),
KW_DESCR ("yield", TOK_KW_YIELD)
#undef KW_DESCR
};
keyword kw = KW_NONE;
jsp_token_type_t kw = TOK_EMPTY;
for (uint32_t i = 0; i < sizeof (keywords) / sizeof (kw_descr_t); i++)
{
@ -608,19 +609,19 @@ lexer_parse_reserved_word (const lit_utf8_byte_t *str_p, /**< characters buffer
}
}
if (!strict_mode)
if (!is_strict)
{
switch (kw)
{
case KW_INTERFACE:
case KW_IMPLEMENTS:
case KW_LET:
case KW_PACKAGE:
case KW_PRIVATE:
case KW_PROTECTED:
case KW_PUBLIC:
case KW_STATIC:
case KW_YIELD:
case TOK_KW_INTERFACE:
case TOK_KW_IMPLEMENTS:
case TOK_KW_LET:
case TOK_KW_PACKAGE:
case TOK_KW_PRIVATE:
case TOK_KW_PROTECTED:
case TOK_KW_PUBLIC:
case TOK_KW_STATIC:
case TOK_KW_YIELD:
{
return empty_token;
}
@ -632,9 +633,9 @@ lexer_parse_reserved_word (const lit_utf8_byte_t *str_p, /**< characters buffer
}
}
if (kw != KW_NONE)
if (kw != TOK_EMPTY)
{
return create_token (TOK_KEYWORD, kw);
return create_token (kw, 0);
}
else
{
@ -736,12 +737,12 @@ consume_char (void)
* Parse Identifier (ECMA-262 v5, 7.6) or ReservedWord (7.6.1; 7.8.1; 7.8.2).
*
* @return TOK_NAME - for Identifier,
* TOK_KEYWORD - for Keyword or FutureReservedWord,
* TOK_KW_* - for Keyword or FutureReservedWord,
* TOK_NULL - for NullLiteral,
* TOK_BOOL - for BooleanLiteral
*/
static token
lexer_parse_identifier_or_keyword (void)
lexer_parse_identifier_or_keyword (bool is_strict) /**< flag, indicating whether current code is in strict mode code */
{
ecma_char_t c = LA (0);
@ -823,8 +824,8 @@ lexer_parse_identifier_or_keyword (void)
if (!is_escape_sequence_occured
&& is_all_chars_were_lowercase_ascii)
{
/* Keyword or FutureReservedWord (TOK_KEYWORD), or boolean literal (TOK_BOOL), or null literal (TOK_NULL) */
ret = lexer_parse_reserved_word (TOK_START (), charset_size);
/* Keyword or FutureReservedWord (TOK_KW_*), or boolean literal (TOK_BOOL), or null literal (TOK_NULL) */
ret = lexer_parse_reserved_word (TOK_START (), charset_size, is_strict);
}
if (is_empty (ret))
@ -856,7 +857,7 @@ lexer_parse_identifier_or_keyword (void)
* @return token of TOK_SMALL_INT or TOK_NUMBER types
*/
static token
lexer_parse_number (void)
lexer_parse_number (bool is_strict) /**< flag, indicating whether current code is in strict mode code */
{
ecma_char_t c = LA (0);
bool is_hex = false;
@ -884,10 +885,11 @@ lexer_parse_number (void)
if (is_hex)
{
new_token ();
// Eat up '0x'
consume_char ();
consume_char ();
new_token ();
c = LA (0);
if (!lit_char_is_hex_digit (c))
@ -911,9 +913,11 @@ lexer_parse_number (void)
tok_length = (size_t) (TOK_SIZE ());
const lit_utf8_byte_t *fp_buf_p = TOK_START ();
const size_t length_of_zero_and_x_str = 2u;
const lit_utf8_byte_t *fp_buf_p = TOK_START () + length_of_zero_and_x_str;
/* token is constructed at end of function */
for (i = 0; i < tok_length; i++)
for (i = 0; i < tok_length - length_of_zero_and_x_str; i++)
{
fp_res = fp_res * 16 + (ecma_number_t) lit_char_hex_to_int (fp_buf_p[i]);
}
@ -1011,7 +1015,7 @@ lexer_parse_number (void)
&& tok_length != 1)
{
/* Octal integer literals */
if (strict_mode)
if (is_strict)
{
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Octal integer literals are not allowed in strict mode", token_start_pos);
}
@ -1262,6 +1266,65 @@ lexer_parse_comment (void)
return false;
} /* lexer_parse_comment */
/**
* Skip any whitespace and comment tokens
*
* @return true - if a newline token was skipped,
* false - otherwise
*/
static bool
lexer_skip_whitespace_and_comments (void)
{
bool new_lines_occurred = false;
while (true)
{
ecma_char_t c = LA (0);
if (lit_char_is_white_space (c))
{
do
{
consume_char ();
c = LA (0);
}
while (lit_char_is_white_space (c));
}
else if (lit_char_is_line_terminator (c))
{
dump_current_line ();
new_lines_occurred = true;
do
{
consume_char ();
c = LA (0);
}
while (lit_char_is_line_terminator (c));
}
else if (c == LIT_CHAR_SLASH
&& (LA (1) == LIT_CHAR_SLASH
|| LA (1) == LIT_CHAR_ASTERISK))
{
/* ECMA-262 v5, 7.4, SingleLineComment or MultiLineComment */
if (lexer_parse_comment ())
{
new_lines_occurred = true;
}
}
else
{
break;
}
}
return new_lines_occurred;
} /* lexer_skip_whitespace_and_comments */
/**
* Parse and construct lexer token
*
@ -1277,38 +1340,21 @@ lexer_parse_comment (void)
* @return constructed token
*/
static token
lexer_parse_token (bool maybe_regexp) /**< read '/' as regexp? */
lexer_parse_token (bool maybe_regexp, /**< read '/' as regexp? */
bool *out_is_preceed_by_new_lines_p, /**< out: is constructed token preceded by newlines? */
bool is_strict) /**< flag, indicating whether current code is in strict mode code */
{
ecma_char_t c = LA (0);
if (lit_char_is_white_space (c))
{
while (lit_char_is_white_space (c))
{
consume_char ();
c = LA (0);
}
}
if (lit_char_is_line_terminator (c))
{
while (lit_char_is_line_terminator (c))
{
consume_char ();
c = LA (0);
}
return create_token (TOK_NEWLINE, 0);
}
JERRY_ASSERT (is_token_parse_in_progress == false);
*out_is_preceed_by_new_lines_p = lexer_skip_whitespace_and_comments ();
ecma_char_t c = LA (0);
/* ECMA-262 v5, 7.6, Identifier */
if (lexer_is_char_can_be_identifier_start (c))
{
return lexer_parse_identifier_or_keyword ();
return lexer_parse_identifier_or_keyword (is_strict);
}
/* ECMA-262 v5, 7.8.3, Numeric literal */
@ -1316,13 +1362,7 @@ lexer_parse_token (bool maybe_regexp) /**< read '/' as regexp? */
|| (c == LIT_CHAR_DOT
&& lit_char_is_decimal_digit (LA (1))))
{
return lexer_parse_number ();
}
if (c == LIT_CHAR_LF)
{
consume_char ();
return create_token (TOK_NEWLINE, 0);
return lexer_parse_number (is_strict);
}
if (c == LIT_CHAR_NULL)
@ -1336,21 +1376,6 @@ lexer_parse_token (bool maybe_regexp) /**< read '/' as regexp? */
return lexer_parse_string ();
}
/* ECMA-262 v5, 7.4, SingleLineComment or MultiLineComment */
if (c == LIT_CHAR_SLASH
&& (LA (1) == LIT_CHAR_SLASH
|| LA (1) == LIT_CHAR_ASTERISK))
{
if (lexer_parse_comment ())
{
return create_token (TOK_NEWLINE, 0);
}
else
{
return lexer_parse_token (maybe_regexp);
}
}
if (c == LIT_CHAR_SLASH && maybe_regexp)
{
return lexer_parse_regexp ();
@ -1518,8 +1543,14 @@ lexer_parse_token (bool maybe_regexp) /**< read '/' as regexp? */
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Illegal character", lit_utf8_iterator_get_pos (&src_iter));
} /* lexer_parse_token */
/**
* Construct next token from current source code position and increment the position
*
* @return the constructed token
*/
token
lexer_next_token (bool maybe_regexp) /**< read '/' as regexp? */
lexer_next_token (bool maybe_regexp, /**< read '/' as regexp? */
bool is_strict) /**< strict mode is on (true) / off (false) */
{
lit_utf8_iterator_pos_t src_pos = lit_utf8_iterator_get_pos (&src_iter);
if (src_pos.offset == 0 && !src_pos.is_non_bmp_middle)
@ -1531,61 +1562,52 @@ lexer_next_token (bool maybe_regexp) /**< read '/' as regexp? */
{
sent_token = saved_token;
saved_token = empty_token;
goto end;
}
/**
* FIXME:
* The way to raise syntax errors for unexpected EOF
* should be reworked so that EOF would be checked by
* caller of the routine, and the following condition
* would be checked as assertion in the routine.
*/
if (prev_token.type == TOK_EOF
&& sent_token.type == TOK_EOF)
{
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unexpected EOF", lit_utf8_iterator_get_pos (&src_iter));
}
prev_token = sent_token;
sent_token = lexer_parse_token (maybe_regexp);
if (sent_token.type == TOK_NEWLINE)
{
dump_current_line ();
}
else
{
prev_non_lf_token = sent_token;
/**
* FIXME:
* The way to raise syntax errors for unexpected EOF
* should be reworked so that EOF would be checked by
* caller of the routine, and the following condition
* would be checked as assertion in the routine.
*/
if (lexer_get_token_type (prev_token) == TOK_EOF
&& lexer_get_token_type (sent_token) == TOK_EOF)
{
PARSE_ERROR (JSP_EARLY_ERROR_SYNTAX, "Unexpected EOF", lit_utf8_iterator_get_pos (&src_iter));
}
prev_token = sent_token;
jsp_token_flag_t flags = JSP_TOKEN_FLAG__NO_FLAGS;
bool is_preceded_by_new_lines;
sent_token = lexer_parse_token (maybe_regexp, &is_preceded_by_new_lines, is_strict);
if (is_preceded_by_new_lines)
{
flags = (jsp_token_flag_t) (flags | JSP_TOKEN_FLAG_PRECEDED_BY_NEWLINES);
}
sent_token.flags = flags;
}
end:
return sent_token;
}
} /* lexer_next_token */
/**
* Set lexer's iteraror over source file to the specified position
*/
void
lexer_save_token (token tok)
{
JERRY_ASSERT (is_empty (saved_token));
saved_token = tok;
prev_non_lf_token = tok;
}
token
lexer_prev_token (void)
{
return prev_token;
}
void
lexer_seek (lit_utf8_iterator_pos_t locus)
lexer_seek (lit_utf8_iterator_pos_t locus) /**< position in the source buffer */
{
JERRY_ASSERT (is_token_parse_in_progress == false);
lit_utf8_iterator_seek (&src_iter, locus);
saved_token = empty_token;
prev_non_lf_token = empty_token;
}
prev_token = empty_token;
} /* lexer_seek */
/**
* Convert locus to line and column
@ -1676,80 +1698,24 @@ lexer_dump_line (size_t line) /**< line number */
}
} /* lexer_dump_line */
/**
* Convert token type to string
*
* @return string, describing token type
*/
const char *
lexer_keyword_to_string (keyword kw)
{
switch (kw)
{
case KW_BREAK: return "break";
case KW_CASE: return "case";
case KW_CATCH: return "catch";
case KW_CLASS: return "class";
case KW_CONST: return "const";
case KW_CONTINUE: return "continue";
case KW_DEBUGGER: return "debugger";
case KW_DEFAULT: return "default";
case KW_DELETE: return "delete";
case KW_DO: return "do";
case KW_ELSE: return "else";
case KW_ENUM: return "enum";
case KW_EXPORT: return "export";
case KW_EXTENDS: return "extends";
case KW_FINALLY: return "finally";
case KW_FOR: return "for";
case KW_FUNCTION: return "function";
case KW_IF: return "if";
case KW_IN: return "in";
case KW_INSTANCEOF: return "instanceof";
case KW_INTERFACE: return "interface";
case KW_IMPORT: return "import";
case KW_IMPLEMENTS: return "implements";
case KW_LET: return "let";
case KW_NEW: return "new";
case KW_PACKAGE: return "package";
case KW_PRIVATE: return "private";
case KW_PROTECTED: return "protected";
case KW_PUBLIC: return "public";
case KW_RETURN: return "return";
case KW_STATIC: return "static";
case KW_SUPER: return "super";
case KW_SWITCH: return "switch";
case KW_THIS: return "this";
case KW_THROW: return "throw";
case KW_TRY: return "try";
case KW_TYPEOF: return "typeof";
case KW_VAR: return "var";
case KW_VOID: return "void";
case KW_WHILE: return "while";
case KW_WITH: return "with";
case KW_YIELD: return "yield";
default: JERRY_UNREACHABLE ();
}
}
const char *
lexer_token_type_to_string (token_type tt)
lexer_token_type_to_string (jsp_token_type_t tt)
{
switch (tt)
{
case TOK_EOF: return "End of file";
case TOK_NAME: return "Identifier";
case TOK_KEYWORD: return "Keyword";
case TOK_SMALL_INT: /* FALLTHRU */
case TOK_NUMBER: return "Number";
case TOK_REGEXP: return "RegExp";
case TOK_NULL: return "null";
case TOK_BOOL: return "bool";
case TOK_NEWLINE: return "newline";
case TOK_STRING: return "string";
case TOK_OPEN_BRACE: return "{";
@ -1809,15 +1775,78 @@ lexer_token_type_to_string (token_type tt)
case TOK_DIV: return "/";
case TOK_DIV_EQ: return "/=";
case TOK_KW_BREAK: return "break";
case TOK_KW_CASE: return "case";
case TOK_KW_CATCH: return "catch";
case TOK_KW_CLASS: return "class";
case TOK_KW_CONST: return "const";
case TOK_KW_CONTINUE: return "continue";
case TOK_KW_DEBUGGER: return "debugger";
case TOK_KW_DEFAULT: return "default";
case TOK_KW_DELETE: return "delete";
case TOK_KW_DO: return "do";
case TOK_KW_ELSE: return "else";
case TOK_KW_ENUM: return "enum";
case TOK_KW_EXPORT: return "export";
case TOK_KW_EXTENDS: return "extends";
case TOK_KW_FINALLY: return "finally";
case TOK_KW_FOR: return "for";
case TOK_KW_FUNCTION: return "function";
case TOK_KW_IF: return "if";
case TOK_KW_IN: return "in";
case TOK_KW_INSTANCEOF: return "instanceof";
case TOK_KW_INTERFACE: return "interface";
case TOK_KW_IMPORT: return "import";
case TOK_KW_IMPLEMENTS: return "implements";
case TOK_KW_LET: return "let";
case TOK_KW_NEW: return "new";
case TOK_KW_PACKAGE: return "package";
case TOK_KW_PRIVATE: return "private";
case TOK_KW_PROTECTED: return "protected";
case TOK_KW_PUBLIC: return "public";
case TOK_KW_RETURN: return "return";
case TOK_KW_STATIC: return "static";
case TOK_KW_SUPER: return "super";
case TOK_KW_SWITCH: return "switch";
case TOK_KW_THIS: return "this";
case TOK_KW_THROW: return "throw";
case TOK_KW_TRY: return "try";
case TOK_KW_TYPEOF: return "typeof";
case TOK_KW_VAR: return "var";
case TOK_KW_VOID: return "void";
case TOK_KW_WHILE: return "while";
case TOK_KW_WITH: return "with";
case TOK_KW_YIELD: return "yield";
default: JERRY_UNREACHABLE ();
}
}
} /* lexer_token_type_to_string */
void
lexer_set_strict_mode (bool is_strict)
/**
* Get type of specified token
*
* @return token type
*/
jsp_token_type_t __attr_always_inline___
lexer_get_token_type (token t) /**< the token */
{
strict_mode = is_strict;
}
JERRY_ASSERT (t.type >= TOKEN_TYPE__BEGIN && t.type <= TOKEN_TYPE__END);
return (jsp_token_type_t) t.type;
} /* lexer_get_token_type */
bool __attr_always_inline___
lexer_is_preceded_by_newlines (token t)
{
return ((t.flags & JSP_TOKEN_FLAG_PRECEDED_BY_NEWLINES) != 0);
} /* lexer_is_preceded_by_newlines */
/**
* Check whether the identifier tokens represent the same identifiers
@ -1833,8 +1862,8 @@ bool
lexer_are_tokens_with_same_identifier (token id1, /**< identifier token (TOK_NAME) */
token id2) /**< identifier token (TOK_NAME) */
{
JERRY_ASSERT (id1.type == TOK_NAME);
JERRY_ASSERT (id2.type == TOK_NAME);
JERRY_ASSERT (lexer_get_token_type (id1) == TOK_NAME);
JERRY_ASSERT (lexer_get_token_type (id2) == TOK_NAME);
return (id1.uid == id2.uid);
} /* lexer_are_tokens_with_same_identifier */
@ -1848,7 +1877,7 @@ lexer_are_tokens_with_same_identifier (token id1, /**< identifier token (TOK_NAM
bool
lexer_is_no_escape_sequences_in_token_string (token tok) /**< token of type TOK_STRING */
{
JERRY_ASSERT (tok.type == TOK_STRING);
JERRY_ASSERT (lexer_get_token_type (tok) == TOK_STRING);
lit_utf8_iterator_t iter = src_iter;
lit_utf8_iterator_seek (&iter, tok.loc);
@ -1888,7 +1917,7 @@ lexer_init (const jerry_api_char_t *source, /**< script source */
empty_token.uid = 0;
empty_token.loc = LIT_ITERATOR_POS_ZERO;
saved_token = prev_token = sent_token = prev_non_lf_token = empty_token;
saved_token = prev_token = sent_token = empty_token;
if (!lit_is_utf8_string_valid (source, (lit_utf8_size_t) source_size))
{
@ -1901,8 +1930,6 @@ lexer_init (const jerry_api_char_t *source, /**< script source */
buffer_start = source;
is_token_parse_in_progress = false;
lexer_set_strict_mode (false);
#ifndef JERRY_NDEBUG
allow_dump_lines = is_print_source_code;
#else /* JERRY_NDEBUG */

View File

@ -25,7 +25,6 @@
typedef enum __attr_packed___
{
/* Not a keyword. */
KW_NONE = 0,
KW_BREAK,
KW_CASE,
KW_CATCH,
@ -82,15 +81,15 @@ typedef enum __attr_packed___
/* Type of tokens. */
typedef enum __attr_packed___
{
TOK_EOF = 0, // End of file
TOKEN_TYPE__BEGIN = 1,
TOK_EOF = TOKEN_TYPE__BEGIN, // End of file
TOK_NAME, // Identifier
TOK_KEYWORD, // Keyword
TOK_SMALL_INT,
TOK_NUMBER,
TOK_NULL,
TOK_BOOL,
TOK_NEWLINE,
TOK_STRING,
TOK_OPEN_BRACE, // {
@ -103,85 +102,178 @@ typedef enum __attr_packed___
TOK_DOT, // .
TOK_SEMICOLON, // ;
TOK_COMMA, // ,
TOK_LESS, // <
TOK_GREATER, // >
TOK_LESS_EQ, // <=
TOK_GREATER_EQ, // <=
TOK_DOUBLE_EQ, // ==
TOK_NOT_EQ, // !=
TOK_TRIPLE_EQ, // ===
TOKEN_TYPE__UNARY_BEGIN,
TOKEN_TYPE__ADDITIVE_BEGIN = TOKEN_TYPE__UNARY_BEGIN,
TOK_NOT_DOUBLE_EQ, // !==
TOK_PLUS, // +
TOK_PLUS = TOKEN_TYPE__ADDITIVE_BEGIN, // +
TOK_MINUS, // -
TOK_MULT, // *
TOK_MOD, // %
TOKEN_TYPE__ADDITIVE_END = TOK_MINUS,
TOK_DOUBLE_PLUS, // ++
TOK_DOUBLE_MINUS, // --
TOK_LSHIFT, // <<
TOK_NOT, // !
TOK_COMPL, // ~
TOKEN_TYPE__UNARY_END = TOK_COMPL, /* keywords are not listed
* in the range */
TOKEN_TYPE__MULTIPLICATIVE_BEGIN,
TOK_MULT = TOKEN_TYPE__MULTIPLICATIVE_BEGIN, // *
TOK_MOD, // %
TOK_DIV, // /
TOKEN_TYPE__MULTIPLICATIVE_END = TOK_DIV,
TOKEN_TYPE__SHIFT_BEGIN,
TOK_LSHIFT = TOKEN_TYPE__SHIFT_BEGIN, // <<
TOK_RSHIFT, // >>
TOK_RSHIFT_EX, // >>>
TOKEN_TYPE__SHIFT_END = TOK_RSHIFT_EX,
TOKEN_TYPE__RELATIONAL_BEGIN,
TOK_LESS = TOKEN_TYPE__RELATIONAL_BEGIN, // <
TOK_GREATER, // >
TOK_LESS_EQ, // <=
TOK_GREATER_EQ, // <=
TOKEN_TYPE__RELATIONAL_END = TOK_GREATER_EQ,
TOKEN_TYPE__EQUALITY_BEGIN,
TOK_DOUBLE_EQ = TOKEN_TYPE__EQUALITY_BEGIN, // ==
TOK_NOT_EQ, // !=
TOK_TRIPLE_EQ, // ===
TOK_NOT_DOUBLE_EQ, // !==
TOKEN_TYPE__EQUALITY_END = TOK_NOT_DOUBLE_EQ,
TOK_AND, // &
TOK_OR, // |
TOK_XOR, // ^
TOK_NOT, // !
TOK_COMPL, // ~
TOK_DOUBLE_AND, // &&
TOK_DOUBLE_OR, // ||
TOK_QUERY, // ?
TOK_COLON, // :
TOK_EQ, // =
TOKEN_TYPE__ASSIGNMENTS_BEGIN,
TOK_EQ = TOKEN_TYPE__ASSIGNMENTS_BEGIN, // =
TOK_PLUS_EQ, // +=
TOK_MINUS_EQ, // -=
TOK_MULT_EQ, // *=
TOK_MOD_EQ, // %=
TOK_LSHIFT_EQ, // <<=
TOK_RSHIFT_EQ, // >>=
TOK_RSHIFT_EX_EQ, // >>>=
TOK_AND_EQ, // &=
TOK_OR_EQ, // |=
TOK_XOR_EQ, // ^=
TOK_DIV, // /
TOK_DIV_EQ, // /=
TOKEN_TYPE__ASSIGNMENTS_END = TOK_DIV_EQ,
TOK_EMPTY,
TOK_REGEXP, // RegularExpressionLiteral (/.../gim)
} token_type;
TOKEN_TYPE__KEYWORD_BEGIN,
TOK_KW_BREAK,
TOK_KW_CASE,
TOK_KW_CATCH,
TOK_KW_CLASS,
TOK_KW_CONST,
TOK_KW_CONTINUE,
TOK_KW_DEBUGGER,
TOK_KW_DEFAULT,
TOK_KW_DELETE,
TOK_KW_DO,
TOK_KW_ELSE,
TOK_KW_ENUM,
TOK_KW_EXPORT,
TOK_KW_EXTENDS,
TOK_KW_FINALLY,
TOK_KW_FOR,
TOK_KW_FUNCTION,
TOK_KW_IF,
TOK_KW_IN,
TOK_KW_INSTANCEOF,
TOK_KW_INTERFACE,
TOK_KW_IMPORT,
TOK_KW_IMPLEMENTS,
TOK_KW_LET,
TOK_KW_NEW,
TOK_KW_PACKAGE,
TOK_KW_PRIVATE,
TOK_KW_PROTECTED,
TOK_KW_PUBLIC,
TOK_KW_RETURN,
TOK_KW_STATIC,
TOK_KW_SUPER,
TOK_KW_SWITCH,
TOK_KW_THIS,
TOK_KW_THROW,
TOK_KW_TRY,
TOK_KW_TYPEOF,
TOK_KW_VAR,
TOK_KW_VOID,
TOK_KW_WHILE,
TOK_KW_WITH,
TOK_KW_YIELD,
TOKEN_TYPE__KEYWORD_END = TOK_KW_YIELD,
TOKEN_TYPE__END = TOKEN_TYPE__KEYWORD_END
} jsp_token_type_t;
/* Flags, describing token properties */
typedef enum
{
JSP_TOKEN_FLAG__NO_FLAGS = 0x00, /* default flag */
JSP_TOKEN_FLAG_PRECEDED_BY_NEWLINES = 0x01 /* designates that newline precedes the token */
} jsp_token_flag_t;
typedef lit_utf8_iterator_pos_t locus;
/* Represents the contents of a token. */
typedef struct
{
locus loc;
token_type type;
uint16_t uid;
locus loc; /**< token location */
uint16_t uid; /**< encodes token's value, depending on token type */
uint8_t type; /**< token type */
uint8_t flags; /**< token flags */
} token;
/**
* Initializer for empty token
*/
#define TOKEN_EMPTY_INITIALIZER {LIT_ITERATOR_POS_ZERO, TOK_EMPTY, 0}
#define TOKEN_EMPTY_INITIALIZER {LIT_ITERATOR_POS_ZERO, 0, TOK_EMPTY, JSP_TOKEN_FLAG__NO_FLAGS}
void lexer_init (const jerry_api_char_t *, size_t, bool);
token lexer_next_token (bool);
void lexer_save_token (token);
token lexer_prev_token (void);
token lexer_next_token (bool, bool);
void lexer_seek (locus);
void lexer_locus_to_line_and_column (locus, size_t *, size_t *);
void lexer_dump_line (size_t);
const char *lexer_keyword_to_string (keyword);
const char *lexer_token_type_to_string (token_type);
const char *lexer_token_type_to_string (jsp_token_type_t);
void lexer_set_strict_mode (bool);
jsp_token_type_t lexer_get_token_type (token);
bool lexer_is_preceded_by_newlines (token);
extern bool lexer_are_tokens_with_same_identifier (token, token);

File diff suppressed because it is too large Load Diff

View File

@ -17,10 +17,11 @@
#define OPCODES_DUMPER_H
#include "ecma-globals.h"
#include "jsp-internal.h"
#include "lexer.h"
#include "lit-literal.h"
#include "opcodes.h"
#include "scopes-tree.h"
#include "serializer.h"
/**
* Operand (descriptor of value or reference in context of parser)
@ -31,7 +32,13 @@ public:
enum type_t : uint8_t
{
EMPTY, /**< empty operand */
LITERAL, /**< operand contains literal value */
STRING_LITERAL, /**< operand contains string literal value */
NUMBER_LITERAL, /**< operand contains number literal value */
REGEXP_LITERAL, /**< operand contains regexp literal value */
SIMPLE_VALUE, /**< operand contains a simple ecma value */
SMALLINT, /**< operand contains small integer value (less than 256) */
IDENTIFIER, /**< Identifier reference */
THIS_BINDING, /**< ThisBinding operand */
TMP, /**< operand contains byte-code register index */
IDX_CONST, /**< operand contains an integer constant that fits vm_idx_t */
UNKNOWN, /**< operand, representing unknown value that would be rewritten later */
@ -67,6 +74,21 @@ public:
return ret;
} /* make_empty_operand */
/**
* Construct ThisBinding operand
*
* @return constructed operand
*/
static jsp_operand_t
make_this_operand (void)
{
jsp_operand_t ret;
ret._type = jsp_operand_t::THIS_BINDING;
return ret;
} /* make_this_operand */
/**
* Construct unknown operand
*
@ -99,22 +121,130 @@ public:
} /* make_idx_const_operand */
/**
* Construct literal operand
* Construct small integer operand
*
* @return constructed operand
*/
static jsp_operand_t
make_lit_operand (lit_cpointer_t lit_id) /**< literal identifier */
make_smallint_operand (uint8_t integer_value) /**< small integer value */
{
jsp_operand_t ret;
ret._type = jsp_operand_t::SMALLINT;
ret._data.smallint_value = integer_value;
return ret;
} /* make_smallint_operand */
/**
* Construct simple ecma value operand
*
* @return constructed operand
*/
static jsp_operand_t
make_simple_value_operand (ecma_simple_value_t simple_value) /**< simple ecma value */
{
jsp_operand_t ret;
ret._type = jsp_operand_t::SIMPLE_VALUE;
ret._data.simple_value = simple_value;
return ret;
} /* make_simple_value_operand */
/**
* Construct string literal operand
*
* @return constructed operand
*/
static jsp_operand_t
make_string_lit_operand (lit_cpointer_t lit_id) /**< literal identifier */
{
JERRY_ASSERT (lit_id.packed_value != NOT_A_LITERAL.packed_value);
#ifndef JERRY_NDEBUG
literal_t lit = lit_get_literal_by_cp (lit_id);
JERRY_ASSERT (lit->get_type () == LIT_STR_T
|| lit->get_type () == LIT_MAGIC_STR_T
|| lit->get_type () == LIT_MAGIC_STR_EX_T);
#endif /* !JERRY_NDEBUG */
jsp_operand_t ret;
ret._type = jsp_operand_t::STRING_LITERAL;
ret._data.lit_id = lit_id;
return ret;
} /* make_string_lit_operand */
/**
* Construct RegExp literal operand
*
* @return constructed operand
*/
static jsp_operand_t
make_regexp_lit_operand (lit_cpointer_t lit_id) /**< literal identifier */
{
JERRY_ASSERT (lit_id.packed_value != NOT_A_LITERAL.packed_value);
#ifndef JERRY_NDEBUG
literal_t lit = lit_get_literal_by_cp (lit_id);
JERRY_ASSERT (lit->get_type () == LIT_STR_T
|| lit->get_type () == LIT_MAGIC_STR_T
|| lit->get_type () == LIT_MAGIC_STR_EX_T);
#endif /* !JERRY_NDEBUG */
jsp_operand_t ret;
ret._type = jsp_operand_t::REGEXP_LITERAL;
ret._data.lit_id = lit_id;
return ret;
} /* make_regexp_lit_operand */
/**
* Construct number literal operand
*
* @return constructed operand
*/
static jsp_operand_t
make_number_lit_operand (lit_cpointer_t lit_id) /**< literal identifier */
{
JERRY_ASSERT (lit_id.packed_value != NOT_A_LITERAL.packed_value);
#ifndef JERRY_NDEBUG
literal_t lit = lit_get_literal_by_cp (lit_id);
JERRY_ASSERT (lit->get_type () == LIT_NUMBER_T);
#endif /* !JERRY_NDEBUG */
jsp_operand_t ret;
ret._type = jsp_operand_t::NUMBER_LITERAL;
ret._data.lit_id = lit_id;
return ret;
} /* make_number_lit_operand */
/**
* Construct identifier reference operand
*
* @return constructed operand
*/
static jsp_operand_t
make_identifier_operand (lit_cpointer_t lit_id) /**< literal identifier */
{
JERRY_ASSERT (lit_id.packed_value != NOT_A_LITERAL.packed_value);
jsp_operand_t ret;
ret._type = jsp_operand_t::LITERAL;
ret._data.lit_id = lit_id;
ret._type = jsp_operand_t::IDENTIFIER;
ret._data.identifier = lit_id;
return ret;
} /* make_lit_operand */
} /* make_identifier_operand */
/**
* Construct register operand
@ -157,6 +287,19 @@ public:
return (_type == jsp_operand_t::EMPTY);
} /* is_empty_operand */
/**
* Is it ThisBinding operand?
*
* @return true / false
*/
bool
is_this_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::THIS_BINDING);
} /* is_this_operand */
/**
* Is it unknown operand?
*
@ -197,17 +340,93 @@ public:
} /* is_register_operand */
/**
* Is it literal operand?
* Is it simple ecma value operand?
*
* @return true / false
*/
bool
is_literal_operand (void) const
is_simple_value_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::LITERAL);
} /* is_literal_operand */
return (_type == jsp_operand_t::SIMPLE_VALUE);
} /* is_simple_value_operand */
/**
* Is it small integer operand?
*
* @return true / false
*/
bool
is_smallint_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::SMALLINT);
} /* is_smallint_operand */
/**
* Is it number literal operand?
*
* @return true / false
*/
bool
is_number_lit_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::NUMBER_LITERAL);
} /* is_number_lit_operand */
/**
* Is it string literal operand?
*
* @return true / false
*/
bool
is_string_lit_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::STRING_LITERAL);
} /* is_string_lit_operand */
/**
* Is it RegExp literal operand?
*
* @return true / false
*/
bool
is_regexp_lit_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::REGEXP_LITERAL);
} /* is_regexp_lit_operand */
/**
* Is it identifier reference operand?
*
* @return true / false
*/
bool is_identifier_operand (void) const
{
JERRY_ASSERT (_type != jsp_operand_t::UNINITIALIZED);
return (_type == jsp_operand_t::IDENTIFIER);
} /* is_identifier_operand */
/**
* Get string literal - name of Identifier reference
*
* @return literal identifier
*/
lit_cpointer_t get_identifier_name (void) const
{
JERRY_ASSERT (is_identifier_operand ());
return (_data.identifier);
} /* get_identifier_name */
/**
* Get idx for operand
@ -224,10 +443,15 @@ public:
{
return _data.uid;
}
else if (_type == jsp_operand_t::LITERAL)
else if (_type == jsp_operand_t::STRING_LITERAL
|| _type == jsp_operand_t::NUMBER_LITERAL)
{
return VM_IDX_REWRITE_LITERAL_UID;
}
else if (_type == jsp_operand_t::THIS_BINDING)
{
return VM_REG_SPECIAL_THIS_BINDING;
}
else
{
JERRY_ASSERT (_type == jsp_operand_t::EMPTY);
@ -251,7 +475,9 @@ public:
{
return NOT_A_LITERAL;
}
else if (_type == jsp_operand_t::LITERAL)
else if (_type == jsp_operand_t::STRING_LITERAL
|| _type == jsp_operand_t::NUMBER_LITERAL
|| _type == jsp_operand_t::REGEXP_LITERAL)
{
return _data.lit_id;
}
@ -275,17 +501,48 @@ public:
return _data.idx_const;
} /* get_idx_const */
/**
* Get small integer constant from operand
*
* @return an integer
*/
uint8_t
get_smallint_value (void) const
{
JERRY_ASSERT (is_smallint_operand ());
return _data.smallint_value;
} /* get_smallint_value */
/**
* Get simple value from operand
*
* @return a simple ecma value
*/
ecma_simple_value_t
get_simple_value (void) const
{
JERRY_ASSERT (is_simple_value_operand ());
return (ecma_simple_value_t) _data.simple_value;
} /* get_simple_value */
private:
union
{
vm_idx_t idx_const; /**< idx constant value (for jsp_operand_t::IDX_CONST) */
vm_idx_t uid; /**< byte-code register index (for jsp_operand_t::TMP) */
vm_idx_t uid; /**< register index (for jsp_operand_t::TMP) */
lit_cpointer_t lit_id; /**< literal (for jsp_operand_t::LITERAL) */
lit_cpointer_t identifier; /**< Identifier reference (is_value_based_ref flag not set) */
uint8_t smallint_value; /**< small integer value */
uint8_t simple_value; /**< simple ecma value */
} _data;
type_t _type; /**< type of operand */
};
static_assert (sizeof (jsp_operand_t) == 4, "");
typedef enum __attr_packed___
{
VARG_FUNC_DECL,
@ -297,196 +554,88 @@ typedef enum __attr_packed___
} varg_list_type;
jsp_operand_t empty_operand (void);
jsp_operand_t literal_operand (lit_cpointer_t);
jsp_operand_t eval_ret_operand (void);
jsp_operand_t jsp_create_operand_for_in_special_reg (void);
jsp_operand_t tmp_operand (void);
bool operand_is_empty (jsp_operand_t);
void dumper_init (void);
void dumper_free (void);
void dumper_init (jsp_ctx_t *, bool);
void dumper_start_move_of_vars_to_regs ();
bool dumper_start_move_of_args_to_regs (uint32_t args_num);
bool dumper_try_replace_identifier_name_with_reg (scopes_tree, op_meta *);
void dumper_alloc_reg_for_unused_arg (void);
vm_instr_counter_t dumper_get_current_instr_counter (jsp_ctx_t *);
void dumper_new_statement (void);
void dumper_new_scope (void);
void dumper_finish_scope (void);
void dumper_start_varg_code_sequence (void);
void dumper_finish_varg_code_sequence (void);
void dumper_start_move_of_vars_to_regs (jsp_ctx_t *);
bool dumper_start_move_of_args_to_regs (jsp_ctx_t *, uint32_t args_num);
bool dumper_try_replace_identifier_name_with_reg (jsp_ctx_t *, bytecode_data_header_t *, op_meta *);
void dumper_alloc_reg_for_unused_arg (jsp_ctx_t *);
void dumper_new_statement (jsp_ctx_t *);
void dumper_save_reg_alloc_ctx (jsp_ctx_t *, vm_idx_t *, vm_idx_t *);
void dumper_restore_reg_alloc_ctx (jsp_ctx_t *, vm_idx_t, vm_idx_t, bool);
vm_idx_t dumper_save_reg_alloc_counter (jsp_ctx_t *);
void dumper_restore_reg_alloc_counter (jsp_ctx_t *, vm_idx_t);
extern bool dumper_is_eval_literal (jsp_operand_t);
jsp_operand_t dump_array_hole_assignment_res (void);
void dump_boolean_assignment (jsp_operand_t, bool);
jsp_operand_t dump_boolean_assignment_res (bool);
void dump_string_assignment (jsp_operand_t, lit_cpointer_t);
jsp_operand_t dump_string_assignment_res (lit_cpointer_t);
void dump_number_assignment (jsp_operand_t, lit_cpointer_t);
jsp_operand_t dump_number_assignment_res (lit_cpointer_t);
void dump_regexp_assignment (jsp_operand_t, lit_cpointer_t);
jsp_operand_t dump_regexp_assignment_res (lit_cpointer_t);
void dump_smallint_assignment (jsp_operand_t, vm_idx_t);
jsp_operand_t dump_smallint_assignment_res (vm_idx_t);
void dump_undefined_assignment (jsp_operand_t);
jsp_operand_t dump_undefined_assignment_res (void);
void dump_null_assignment (jsp_operand_t);
jsp_operand_t dump_null_assignment_res (void);
void dump_variable_assignment (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_variable_assignment_res (jsp_operand_t);
void dump_variable_assignment (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void dump_varg_header_for_rewrite (varg_list_type, jsp_operand_t);
jsp_operand_t rewrite_varg_header_set_args_count (size_t);
void dump_call_additional_info (opcode_call_flags_t, jsp_operand_t);
void dump_varg (jsp_operand_t);
vm_instr_counter_t dump_varg_header_for_rewrite (jsp_ctx_t *, varg_list_type, jsp_operand_t, jsp_operand_t);
void rewrite_varg_header_set_args_count (jsp_ctx_t *, size_t, vm_instr_counter_t);
void dump_call_additional_info (jsp_ctx_t *, opcode_call_flags_t, jsp_operand_t);
void dump_varg (jsp_ctx_t *, jsp_operand_t);
void dump_prop_name_and_value (jsp_operand_t, jsp_operand_t);
void dump_prop_getter_decl (jsp_operand_t, jsp_operand_t);
void dump_prop_setter_decl (jsp_operand_t, jsp_operand_t);
void dump_prop_getter (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_getter_res (jsp_operand_t, jsp_operand_t);
void dump_prop_setter (jsp_operand_t, jsp_operand_t, jsp_operand_t);
void dump_prop_name_and_value (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void dump_prop_getter_decl (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void dump_prop_setter_decl (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void dump_prop_getter (jsp_ctx_t *, jsp_operand_t, jsp_operand_t, jsp_operand_t);
void dump_prop_setter (jsp_ctx_t *, jsp_operand_t, jsp_operand_t, jsp_operand_t);
void dump_function_end_for_rewrite (void);
void rewrite_function_end ();
void dumper_decrement_function_end_pos (void);
vm_instr_counter_t dump_conditional_check_for_rewrite (jsp_ctx_t *, jsp_operand_t);
void rewrite_conditional_check (jsp_ctx_t *, vm_instr_counter_t);
vm_instr_counter_t dump_jump_to_end_for_rewrite (jsp_ctx_t *);
void rewrite_jump_to_end (jsp_ctx_t *, vm_instr_counter_t);
jsp_operand_t dump_this_res (void);
vm_instr_counter_t dumper_set_next_iteration_target (jsp_ctx_t *);
vm_instr_counter_t dump_simple_or_nested_jump_for_rewrite (jsp_ctx_t *,
bool,
bool,
bool,
jsp_operand_t,
vm_instr_counter_t);
vm_instr_counter_t rewrite_simple_or_nested_jump_and_get_next (jsp_ctx_t *, vm_instr_counter_t, vm_instr_counter_t);
void dump_continue_iterations_check (jsp_ctx_t *, vm_instr_counter_t, jsp_operand_t);
void dump_post_increment (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_post_increment_res (jsp_operand_t);
void dump_post_decrement (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_post_decrement_res (jsp_operand_t);
void dump_pre_increment (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_pre_increment_res (jsp_operand_t);
void dump_pre_decrement (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_pre_decrement_res (jsp_operand_t);
void dump_unary_plus (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_unary_plus_res (jsp_operand_t);
void dump_unary_minus (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_unary_minus_res (jsp_operand_t);
void dump_bitwise_not (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_bitwise_not_res (jsp_operand_t);
void dump_logical_not (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_logical_not_res (jsp_operand_t);
void dump_delete (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void dump_delete_prop (jsp_ctx_t *, jsp_operand_t, jsp_operand_t, jsp_operand_t);
void dump_multiplication (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_multiplication_res (jsp_operand_t, jsp_operand_t);
void dump_division (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_division_res (jsp_operand_t, jsp_operand_t);
void dump_remainder (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_remainder_res (jsp_operand_t, jsp_operand_t);
void dump_addition (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_addition_res (jsp_operand_t, jsp_operand_t);
void dump_substraction (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_substraction_res (jsp_operand_t, jsp_operand_t);
void dump_left_shift (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_left_shift_res (jsp_operand_t, jsp_operand_t);
void dump_right_shift (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_right_shift_res (jsp_operand_t, jsp_operand_t);
void dump_right_shift_ex (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_right_shift_ex_res (jsp_operand_t, jsp_operand_t);
void dump_less_than (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_less_than_res (jsp_operand_t, jsp_operand_t);
void dump_greater_than (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_greater_than_res (jsp_operand_t, jsp_operand_t);
void dump_less_or_equal_than (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_less_or_equal_than_res (jsp_operand_t, jsp_operand_t);
void dump_greater_or_equal_than (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_greater_or_equal_than_res (jsp_operand_t, jsp_operand_t);
void dump_instanceof (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_instanceof_res (jsp_operand_t, jsp_operand_t);
void dump_in (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_in_res (jsp_operand_t, jsp_operand_t);
void dump_equal_value (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_equal_value_res (jsp_operand_t, jsp_operand_t);
void dump_not_equal_value (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_not_equal_value_res (jsp_operand_t, jsp_operand_t);
void dump_equal_value_type (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_equal_value_type_res (jsp_operand_t, jsp_operand_t);
void dump_not_equal_value_type (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_not_equal_value_type_res (jsp_operand_t, jsp_operand_t);
void dump_bitwise_and (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_bitwise_and_res (jsp_operand_t, jsp_operand_t);
void dump_bitwise_xor (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_bitwise_xor_res (jsp_operand_t, jsp_operand_t);
void dump_bitwise_or (jsp_operand_t, jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_bitwise_or_res (jsp_operand_t, jsp_operand_t);
void dump_typeof (jsp_ctx_t *, jsp_operand_t, jsp_operand_t);
void start_dumping_logical_and_checks (void);
void dump_logical_and_check_for_rewrite (jsp_operand_t);
void rewrite_logical_and_checks (void);
void start_dumping_logical_or_checks (void);
void dump_logical_or_check_for_rewrite (jsp_operand_t);
void rewrite_logical_or_checks (void);
void dump_conditional_check_for_rewrite (jsp_operand_t);
void rewrite_conditional_check (void);
void dump_jump_to_end_for_rewrite (void);
void rewrite_jump_to_end (void);
void dump_unary_op (jsp_ctx_t *, vm_op_t, jsp_operand_t, jsp_operand_t);
void dump_binary_op (jsp_ctx_t *, vm_op_t, jsp_operand_t, jsp_operand_t, jsp_operand_t);
void start_dumping_assignment_expression (jsp_operand_t, locus);
jsp_operand_t dump_prop_setter_or_variable_assignment_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_addition_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_multiplication_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_division_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_remainder_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_substraction_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_left_shift_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_right_shift_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_right_shift_ex_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_bitwise_and_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_bitwise_xor_res (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_prop_setter_or_bitwise_or_res (jsp_operand_t, jsp_operand_t);
vm_instr_counter_t dump_with_for_rewrite (jsp_ctx_t *, jsp_operand_t);
void rewrite_with (jsp_ctx_t *, vm_instr_counter_t);
void dump_with_end (jsp_ctx_t *);
void dumper_set_break_target (void);
void dumper_set_continue_target (void);
void dumper_set_next_interation_target (void);
vm_instr_counter_t
dump_simple_or_nested_jump_for_rewrite (bool, vm_instr_counter_t);
vm_instr_counter_t
rewrite_simple_or_nested_jump_and_get_next (vm_instr_counter_t, vm_instr_counter_t);
void dump_continue_iterations_check (jsp_operand_t);
vm_instr_counter_t dump_for_in_for_rewrite (jsp_ctx_t *, jsp_operand_t);
void rewrite_for_in (jsp_ctx_t *, vm_instr_counter_t);
void dump_for_in_end (jsp_ctx_t *);
void start_dumping_case_clauses (void);
void dump_case_clause_check_for_rewrite (jsp_operand_t, jsp_operand_t);
void dump_default_clause_check_for_rewrite (void);
void rewrite_case_clause (void);
void rewrite_default_clause (void);
void finish_dumping_case_clauses (void);
vm_instr_counter_t dump_try_for_rewrite (jsp_ctx_t *);
vm_instr_counter_t dump_catch_for_rewrite (jsp_ctx_t *, jsp_operand_t);
vm_instr_counter_t dump_finally_for_rewrite (jsp_ctx_t *);
void rewrite_try (jsp_ctx_t *, vm_instr_counter_t);
void rewrite_catch (jsp_ctx_t *, vm_instr_counter_t);
void rewrite_finally (jsp_ctx_t *, vm_instr_counter_t);
void dump_end_try_catch_finally (jsp_ctx_t *);
void dump_throw (jsp_ctx_t *, jsp_operand_t);
void dump_delete (jsp_operand_t, jsp_operand_t, bool, locus);
jsp_operand_t dump_delete_res (jsp_operand_t, bool, locus);
void dump_variable_declaration (jsp_ctx_t *, lit_cpointer_t);
void dump_typeof (jsp_operand_t, jsp_operand_t);
jsp_operand_t dump_typeof_res (jsp_operand_t);
vm_instr_counter_t dump_reg_var_decl_for_rewrite (jsp_ctx_t *);
void rewrite_reg_var_decl (jsp_ctx_t *, vm_instr_counter_t);
vm_instr_counter_t dump_with_for_rewrite (jsp_operand_t);
void rewrite_with (vm_instr_counter_t);
void dump_with_end (void);
void dump_ret (jsp_ctx_t *);
void dump_retval (jsp_ctx_t *, jsp_operand_t);
vm_instr_counter_t dump_for_in_for_rewrite (jsp_operand_t);
void rewrite_for_in (vm_instr_counter_t);
void dump_for_in_end (void);
void dump_try_for_rewrite (void);
void rewrite_try (void);
void dump_catch_for_rewrite (jsp_operand_t);
void rewrite_catch (void);
void dump_finally_for_rewrite (void);
void rewrite_finally (void);
void dump_end_try_catch_finally (void);
void dump_throw (jsp_operand_t);
void dump_variable_declaration (lit_cpointer_t);
vm_instr_counter_t dump_scope_code_flags_for_rewrite (void);
void rewrite_scope_code_flags (vm_instr_counter_t, opcode_scope_code_flags_t);
vm_instr_counter_t dump_reg_var_decl_for_rewrite (void);
void rewrite_reg_var_decl (vm_instr_counter_t);
void dump_ret (void);
void dump_retval (jsp_operand_t);
op_meta dumper_get_op_meta (jsp_ctx_t *, vm_instr_counter_t);
void dumper_rewrite_op_meta (jsp_ctx_t *, vm_instr_counter_t, op_meta);
#endif /* OPCODES_DUMPER_H */

File diff suppressed because it is too large Load Diff

View File

@ -17,11 +17,8 @@
#include "jsp-mm.h"
#include "scopes-tree.h"
#define HASH_SIZE 128
static hash_table lit_id_to_uid = null_hash;
static vm_instr_counter_t global_oc;
static vm_idx_t next_uid;
scopes_tree scopes_tree_root_node_p = NULL;
scopes_tree scopes_tree_last_node_p = NULL;
static void
assert_tree (scopes_tree t)
@ -29,20 +26,6 @@ assert_tree (scopes_tree t)
JERRY_ASSERT (t != NULL);
}
static vm_idx_t
get_uid (op_meta *op, size_t i)
{
JERRY_ASSERT (i < 3);
return op->op.data.raw_args[i];
}
static void
set_uid (op_meta *op, size_t i, vm_idx_t uid)
{
JERRY_ASSERT (i < 3);
op->op.data.raw_args[i] = uid;
}
vm_instr_counter_t
scopes_tree_instrs_num (scopes_tree t)
{
@ -62,13 +45,6 @@ scopes_tree_var_decls_num (scopes_tree t) /**< scope */
return linked_list_get_length (t->var_decls);
} /* scopes_tree_var_decls_num */
void
scopes_tree_add_op_meta (scopes_tree tree, op_meta op)
{
assert_tree (tree);
linked_list_set_element (tree->instrs, tree->instrs_count++, &op);
}
/**
* Add variable declaration to a scope
*/
@ -80,30 +56,6 @@ scopes_tree_add_var_decl (scopes_tree tree, /**< scope, to which variable declar
linked_list_set_element (tree->var_decls, linked_list_get_length (tree->var_decls), &op);
} /* scopes_tree_add_var_decl */
void
scopes_tree_set_op_meta (scopes_tree tree, vm_instr_counter_t oc, op_meta op)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->instrs_count);
linked_list_set_element (tree->instrs, oc, &op);
}
void
scopes_tree_set_instrs_num (scopes_tree tree, vm_instr_counter_t oc)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->instrs_count);
tree->instrs_count = oc;
}
op_meta
scopes_tree_op_meta (scopes_tree tree, vm_instr_counter_t oc)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->instrs_count);
return *(op_meta *) linked_list_element (tree->instrs, oc);
}
/**
* Get variable declaration for the specified scope
*
@ -118,34 +70,6 @@ scopes_tree_var_decl (scopes_tree tree, /**< scope, from which variable declarat
return *(op_meta *) linked_list_element (tree->var_decls, oc);
} /* scopes_tree_var_decl */
/**
* Remove specified instruction from scopes tree node's instructions list
*/
void
scopes_tree_remove_op_meta (scopes_tree tree, /**< scopes tree node */
vm_instr_counter_t oc) /**< position of instruction to remove */
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->instrs_count);
linked_list_remove_element (tree->instrs, oc);
tree->instrs_count--;
} /* scopes_tree_remove_op_meta */
vm_instr_counter_t
scopes_tree_count_instructions (scopes_tree t)
{
assert_tree (t);
vm_instr_counter_t res = (vm_instr_counter_t) (t->instrs_count + linked_list_get_length (t->var_decls));
for (uint8_t i = 0; i < t->t.children_num; i++)
{
res = (vm_instr_counter_t) (
res + scopes_tree_count_instructions (
*(scopes_tree *) linked_list_element (t->t.children, i)));
}
return res;
}
/**
* Checks if variable declaration exists in the scope
*
@ -175,581 +99,20 @@ scopes_tree_variable_declaration_exists (scopes_tree tree, /**< scope */
return false;
} /* scopes_tree_variable_declaration_exists */
static uint16_t
lit_id_hash (void * lit_id)
{
return ((lit_cpointer_t *) lit_id)->packed_value % HASH_SIZE;
}
static void
start_new_block_if_necessary (void)
{
if (global_oc % BLOCK_SIZE == 0)
{
next_uid = 0;
if (lit_id_to_uid != null_hash)
{
hash_table_free (lit_id_to_uid);
lit_id_to_uid = null_hash;
}
lit_id_to_uid = hash_table_init (sizeof (lit_cpointer_t), sizeof (vm_idx_t), HASH_SIZE, lit_id_hash);
}
}
static bool
is_possible_literal (uint16_t mask, uint8_t index)
{
int res;
switch (index)
{
case 0:
{
res = mask >> 8;
break;
}
case 1:
{
res = (mask & 0xF0) >> 4;
break;
}
default:
{
JERRY_ASSERT (index = 2);
res = mask & 0x0F;
}
}
JERRY_ASSERT (res == 0 || res == 1);
return res == 1;
}
static void
change_uid (op_meta *om, lit_id_hash_table *lit_ids, uint16_t mask)
{
for (uint8_t i = 0; i < 3; i++)
{
if (is_possible_literal (mask, i))
{
if (get_uid (om, i) == VM_IDX_REWRITE_LITERAL_UID)
{
JERRY_ASSERT (om->lit_id[i].packed_value != MEM_CP_NULL);
lit_cpointer_t lit_id = om->lit_id[i];
vm_idx_t *uid = (vm_idx_t *) hash_table_lookup (lit_id_to_uid, &lit_id);
if (uid == NULL)
{
hash_table_insert (lit_id_to_uid, &lit_id, &next_uid);
lit_id_hash_table_insert (lit_ids, next_uid, global_oc, lit_id);
uid = (vm_idx_t *) hash_table_lookup (lit_id_to_uid, &lit_id);
JERRY_ASSERT (uid != NULL);
JERRY_ASSERT (*uid == next_uid);
next_uid++;
}
set_uid (om, i, *uid);
}
else
{
JERRY_ASSERT (om->lit_id[i].packed_value == MEM_CP_NULL);
}
}
else
{
JERRY_ASSERT (om->lit_id[i].packed_value == MEM_CP_NULL);
}
}
}
static void
insert_uids_to_lit_id_map (op_meta *om, uint16_t mask)
{
for (uint8_t i = 0; i < 3; i++)
{
if (is_possible_literal (mask, i))
{
if (get_uid (om, i) == VM_IDX_REWRITE_LITERAL_UID)
{
JERRY_ASSERT (om->lit_id[i].packed_value != MEM_CP_NULL);
lit_cpointer_t lit_id = om->lit_id[i];
vm_idx_t *uid = (vm_idx_t *) hash_table_lookup (lit_id_to_uid, &lit_id);
if (uid == NULL)
{
hash_table_insert (lit_id_to_uid, &lit_id, &next_uid);
uid = (vm_idx_t *) hash_table_lookup (lit_id_to_uid, &lit_id);
JERRY_ASSERT (uid != NULL);
JERRY_ASSERT (*uid == next_uid);
next_uid++;
}
}
else
{
JERRY_ASSERT (om->lit_id[i].packed_value == MEM_CP_NULL);
}
}
else
{
JERRY_ASSERT (om->lit_id[i].packed_value == MEM_CP_NULL);
}
}
}
/**
* Get instruction from instruction list
*
* @return instruction at specified position
* Fill variable declaration list of bytecode header
*/
static op_meta *
extract_op_meta (linked_list instr_list, /**< instruction list */
vm_instr_counter_t instr_pos) /**< position inside the list */
void
scopes_tree_dump_var_decls (scopes_tree scope, /**< scopes tree */
lit_cpointer_t *var_decls_p) /**< pointer to bytecode header's declarations table,
* where variables' lit_cp's should be stored */
{
return (op_meta *) linked_list_element (instr_list, instr_pos);
} /* extract_op_meta */
/**
* Add instruction to an instruction list
*
* @return generated instruction
*/
static vm_instr_t
generate_instr (linked_list instr_list, /**< instruction list */
vm_instr_counter_t instr_pos, /**< position where to generate an instruction */
lit_id_hash_table *lit_ids) /**< hash table binding operand identifiers and literals */
{
start_new_block_if_necessary ();
op_meta *om_p = extract_op_meta (instr_list, instr_pos);
/* Now we should change uids of instructions.
Since different instructions has different literals/tmps in different places,
we should change only them.
For each case possible literal positions are shown as 0xYYY literal,
where Y is set to '1' when there is a possible literal in this position,
and '0' otherwise. */
switch (om_p->op.op_idx)
for (uint32_t i = 0; i < scopes_tree_var_decls_num (scope); ++i)
{
case VM_OP_PROP_GETTER:
case VM_OP_PROP_SETTER:
case VM_OP_DELETE_PROP:
case VM_OP_B_SHIFT_LEFT:
case VM_OP_B_SHIFT_RIGHT:
case VM_OP_B_SHIFT_URIGHT:
case VM_OP_B_AND:
case VM_OP_B_OR:
case VM_OP_B_XOR:
case VM_OP_EQUAL_VALUE:
case VM_OP_NOT_EQUAL_VALUE:
case VM_OP_EQUAL_VALUE_TYPE:
case VM_OP_NOT_EQUAL_VALUE_TYPE:
case VM_OP_LESS_THAN:
case VM_OP_GREATER_THAN:
case VM_OP_LESS_OR_EQUAL_THAN:
case VM_OP_GREATER_OR_EQUAL_THAN:
case VM_OP_INSTANCEOF:
case VM_OP_IN:
case VM_OP_ADDITION:
case VM_OP_SUBSTRACTION:
case VM_OP_DIVISION:
case VM_OP_MULTIPLICATION:
case VM_OP_REMAINDER:
{
change_uid (om_p, lit_ids, 0x111);
break;
}
case VM_OP_CALL_N:
case VM_OP_CONSTRUCT_N:
case VM_OP_FUNC_EXPR_N:
case VM_OP_DELETE_VAR:
case VM_OP_TYPEOF:
case VM_OP_B_NOT:
case VM_OP_LOGICAL_NOT:
case VM_OP_POST_INCR:
case VM_OP_POST_DECR:
case VM_OP_PRE_INCR:
case VM_OP_PRE_DECR:
case VM_OP_UNARY_PLUS:
case VM_OP_UNARY_MINUS:
{
change_uid (om_p, lit_ids, 0x110);
break;
}
case VM_OP_ASSIGNMENT:
{
switch (om_p->op.data.assignment.type_value_right)
{
case OPCODE_ARG_TYPE_SIMPLE:
case OPCODE_ARG_TYPE_SMALLINT:
case OPCODE_ARG_TYPE_SMALLINT_NEGATE:
{
change_uid (om_p, lit_ids, 0x100);
break;
}
case OPCODE_ARG_TYPE_NUMBER:
case OPCODE_ARG_TYPE_NUMBER_NEGATE:
case OPCODE_ARG_TYPE_REGEXP:
case OPCODE_ARG_TYPE_STRING:
case OPCODE_ARG_TYPE_VARIABLE:
{
change_uid (om_p, lit_ids, 0x101);
break;
}
}
break;
}
case VM_OP_FUNC_DECL_N:
case VM_OP_ARRAY_DECL:
case VM_OP_OBJ_DECL:
case VM_OP_WITH:
case VM_OP_FOR_IN:
case VM_OP_THROW_VALUE:
case VM_OP_IS_TRUE_JMP_UP:
case VM_OP_IS_TRUE_JMP_DOWN:
case VM_OP_IS_FALSE_JMP_UP:
case VM_OP_IS_FALSE_JMP_DOWN:
case VM_OP_VAR_DECL:
case VM_OP_RETVAL:
{
change_uid (om_p, lit_ids, 0x100);
break;
}
case VM_OP_RET:
case VM_OP_TRY_BLOCK:
case VM_OP_JMP_UP:
case VM_OP_JMP_DOWN:
case VM_OP_REG_VAR_DECL:
{
change_uid (om_p, lit_ids, 0x000);
break;
}
case VM_OP_META:
{
switch (om_p->op.data.meta.type)
{
case OPCODE_META_TYPE_VARG_PROP_DATA:
case OPCODE_META_TYPE_VARG_PROP_GETTER:
case OPCODE_META_TYPE_VARG_PROP_SETTER:
{
change_uid (om_p, lit_ids, 0x011);
break;
}
case OPCODE_META_TYPE_VARG:
case OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER:
{
change_uid (om_p, lit_ids, 0x010);
break;
}
case OPCODE_META_TYPE_UNDEFINED:
case OPCODE_META_TYPE_END_WITH:
case OPCODE_META_TYPE_FUNCTION_END:
case OPCODE_META_TYPE_CATCH:
case OPCODE_META_TYPE_FINALLY:
case OPCODE_META_TYPE_END_TRY_CATCH_FINALLY:
case OPCODE_META_TYPE_CALL_SITE_INFO:
case OPCODE_META_TYPE_SCOPE_CODE_FLAGS:
{
change_uid (om_p, lit_ids, 0x000);
break;
}
}
break;
}
op_meta var_decl_op_meta = *(op_meta *) linked_list_element (scope->var_decls, i);
var_decls_p[i] = var_decl_op_meta.lit_id[0];
}
return om_p->op;
} /* generate_instr */
/**
* Count number of literals in instruction which were not seen previously
*
* @return number of new literals
*/
static vm_idx_t
count_new_literals_in_instr (op_meta *om_p) /**< instruction */
{
start_new_block_if_necessary ();
vm_idx_t current_uid = next_uid;
switch (om_p->op.op_idx)
{
case VM_OP_PROP_GETTER:
case VM_OP_PROP_SETTER:
case VM_OP_DELETE_PROP:
case VM_OP_B_SHIFT_LEFT:
case VM_OP_B_SHIFT_RIGHT:
case VM_OP_B_SHIFT_URIGHT:
case VM_OP_B_AND:
case VM_OP_B_OR:
case VM_OP_B_XOR:
case VM_OP_EQUAL_VALUE:
case VM_OP_NOT_EQUAL_VALUE:
case VM_OP_EQUAL_VALUE_TYPE:
case VM_OP_NOT_EQUAL_VALUE_TYPE:
case VM_OP_LESS_THAN:
case VM_OP_GREATER_THAN:
case VM_OP_LESS_OR_EQUAL_THAN:
case VM_OP_GREATER_OR_EQUAL_THAN:
case VM_OP_INSTANCEOF:
case VM_OP_IN:
case VM_OP_ADDITION:
case VM_OP_SUBSTRACTION:
case VM_OP_DIVISION:
case VM_OP_MULTIPLICATION:
case VM_OP_REMAINDER:
{
insert_uids_to_lit_id_map (om_p, 0x111);
break;
}
case VM_OP_CALL_N:
case VM_OP_CONSTRUCT_N:
case VM_OP_FUNC_EXPR_N:
case VM_OP_DELETE_VAR:
case VM_OP_TYPEOF:
case VM_OP_B_NOT:
case VM_OP_LOGICAL_NOT:
case VM_OP_POST_INCR:
case VM_OP_POST_DECR:
case VM_OP_PRE_INCR:
case VM_OP_PRE_DECR:
case VM_OP_UNARY_PLUS:
case VM_OP_UNARY_MINUS:
{
insert_uids_to_lit_id_map (om_p, 0x110);
break;
}
case VM_OP_ASSIGNMENT:
{
switch (om_p->op.data.assignment.type_value_right)
{
case OPCODE_ARG_TYPE_SIMPLE:
case OPCODE_ARG_TYPE_SMALLINT:
case OPCODE_ARG_TYPE_SMALLINT_NEGATE:
{
insert_uids_to_lit_id_map (om_p, 0x100);
break;
}
case OPCODE_ARG_TYPE_NUMBER:
case OPCODE_ARG_TYPE_NUMBER_NEGATE:
case OPCODE_ARG_TYPE_REGEXP:
case OPCODE_ARG_TYPE_STRING:
case OPCODE_ARG_TYPE_VARIABLE:
{
insert_uids_to_lit_id_map (om_p, 0x101);
break;
}
}
break;
}
case VM_OP_FUNC_DECL_N:
case VM_OP_ARRAY_DECL:
case VM_OP_OBJ_DECL:
case VM_OP_WITH:
case VM_OP_THROW_VALUE:
case VM_OP_IS_TRUE_JMP_UP:
case VM_OP_IS_TRUE_JMP_DOWN:
case VM_OP_IS_FALSE_JMP_UP:
case VM_OP_IS_FALSE_JMP_DOWN:
case VM_OP_VAR_DECL:
case VM_OP_RETVAL:
{
insert_uids_to_lit_id_map (om_p, 0x100);
break;
}
case VM_OP_RET:
case VM_OP_TRY_BLOCK:
case VM_OP_JMP_UP:
case VM_OP_JMP_DOWN:
case VM_OP_REG_VAR_DECL:
{
insert_uids_to_lit_id_map (om_p, 0x000);
break;
}
case VM_OP_META:
{
switch (om_p->op.data.meta.type)
{
case OPCODE_META_TYPE_VARG_PROP_DATA:
case OPCODE_META_TYPE_VARG_PROP_GETTER:
case OPCODE_META_TYPE_VARG_PROP_SETTER:
{
insert_uids_to_lit_id_map (om_p, 0x011);
break;
}
case OPCODE_META_TYPE_VARG:
case OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER:
{
insert_uids_to_lit_id_map (om_p, 0x010);
break;
}
case OPCODE_META_TYPE_UNDEFINED:
case OPCODE_META_TYPE_END_WITH:
case OPCODE_META_TYPE_FUNCTION_END:
case OPCODE_META_TYPE_CATCH:
case OPCODE_META_TYPE_FINALLY:
case OPCODE_META_TYPE_END_TRY_CATCH_FINALLY:
case OPCODE_META_TYPE_CALL_SITE_INFO:
case OPCODE_META_TYPE_SCOPE_CODE_FLAGS:
{
insert_uids_to_lit_id_map (om_p, 0x000);
break;
}
}
break;
}
}
return (vm_idx_t) (next_uid - current_uid);
} /* count_new_literals_in_instr */
/**
* Count slots needed for a scope's hash table
*
* Before filling literal indexes 'hash' table we shall initiate it with number of neccesary literal indexes.
* Since bytecode is divided into blocks and id of the block is a part of hash key, we shall divide bytecode
* into blocks and count unique literal indexes used in each block.
*
* @return total number of literals in scope
*/
size_t
scopes_tree_count_literals_in_blocks (scopes_tree tree) /**< scope */
{
assert_tree (tree);
size_t result = 0;
if (lit_id_to_uid != null_hash)
{
hash_table_free (lit_id_to_uid);
lit_id_to_uid = null_hash;
}
next_uid = 0;
global_oc = 0;
assert_tree (tree);
vm_instr_counter_t instr_pos;
bool header = true;
for (instr_pos = 0; instr_pos < tree->instrs_count; instr_pos++)
{
op_meta *om_p = extract_op_meta (tree->instrs, instr_pos);
if (om_p->op.op_idx != VM_OP_META && !header)
{
break;
}
if (om_p->op.op_idx == VM_OP_REG_VAR_DECL)
{
header = false;
}
result += count_new_literals_in_instr (om_p);
}
for (vm_instr_counter_t var_decl_pos = 0;
var_decl_pos < linked_list_get_length (tree->var_decls);
var_decl_pos++)
{
op_meta *om_p = extract_op_meta (tree->var_decls, var_decl_pos);
result += count_new_literals_in_instr (om_p);
}
for (uint8_t child_id = 0; child_id < tree->t.children_num; child_id++)
{
result += scopes_tree_count_literals_in_blocks (*(scopes_tree *) linked_list_element (tree->t.children, child_id));
}
for (; instr_pos < tree->instrs_count; instr_pos++)
{
op_meta *om_p = extract_op_meta (tree->instrs, instr_pos);
result += count_new_literals_in_instr (om_p);
}
return result;
} /* scopes_tree_count_literals_in_blocks */
/*
* This function performs functions hoisting.
*
* Each scope consists of four parts:
* 1) Header with 'use strict' marker and reg_var_decl opcode
* 2) Variable declarations, dumped by the preparser
* 3) Function declarations
* 4) Computational code
*
* Header and var_decls are dumped first,
* then we shall recursively dump function declaration,
* and finally, other instructions.
*
* For each instructions block (size of block is defined in bytecode-data.h)
* literal indexes 'hash' table is filled.
*/
static void
merge_subscopes (scopes_tree tree, /**< scopes tree to merge */
vm_instr_t *data_p, /**< instruction array, where the scopes are merged to */
lit_id_hash_table *lit_ids_p) /**< literal indexes 'hash' table */
{
assert_tree (tree);
JERRY_ASSERT (data_p);
vm_instr_counter_t instr_pos;
bool header = true;
for (instr_pos = 0; instr_pos < tree->instrs_count; instr_pos++)
{
op_meta *om_p = extract_op_meta (tree->instrs, instr_pos);
if (om_p->op.op_idx != VM_OP_VAR_DECL
&& om_p->op.op_idx != VM_OP_META && !header)
{
break;
}
if (om_p->op.op_idx == VM_OP_REG_VAR_DECL)
{
header = false;
}
data_p[global_oc] = generate_instr (tree->instrs, instr_pos, lit_ids_p);
global_oc++;
}
for (vm_instr_counter_t var_decl_pos = 0;
var_decl_pos < linked_list_get_length (tree->var_decls);
var_decl_pos++)
{
data_p[global_oc] = generate_instr (tree->var_decls, var_decl_pos, lit_ids_p);
global_oc++;
}
for (uint8_t child_id = 0; child_id < tree->t.children_num; child_id++)
{
merge_subscopes (*(scopes_tree *) linked_list_element (tree->t.children, child_id),
data_p, lit_ids_p);
}
for (; instr_pos < tree->instrs_count; instr_pos++)
{
data_p[global_oc] = generate_instr (tree->instrs, instr_pos, lit_ids_p);
global_oc++;
}
} /* merge_subscopes */
/* Postparser.
Init literal indexes 'hash' table.
Reorder function declarations.
Rewrite instructions' temporary uids with their keys in literal indexes 'hash' table. */
vm_instr_t *
scopes_tree_raw_data (scopes_tree tree, /**< scopes tree to convert to byte-code array */
uint8_t *buffer_p, /**< buffer for byte-code array and literal identifiers hash table */
size_t instructions_array_size, /**< size of space for byte-code array */
lit_id_hash_table *lit_ids) /**< literal identifiers hash table */
{
JERRY_ASSERT (lit_ids);
assert_tree (tree);
if (lit_id_to_uid != null_hash)
{
hash_table_free (lit_id_to_uid);
lit_id_to_uid = null_hash;
}
next_uid = 0;
global_oc = 0;
/* Dump bytecode and fill literal indexes 'hash' table. */
JERRY_ASSERT (instructions_array_size >= (size_t) (scopes_tree_count_instructions (tree)) * sizeof (vm_instr_t));
vm_instr_t *instrs = (vm_instr_t *) buffer_p;
memset (instrs, 0, instructions_array_size);
merge_subscopes (tree, instrs, lit_ids);
if (lit_id_to_uid != null_hash)
{
hash_table_free (lit_id_to_uid);
lit_id_to_uid = null_hash;
}
return instrs;
} /* scopes_tree_raw_data */
} /* scopes_tree_dump_var_decls */
/**
* Set up a flag, indicating that scope should be executed in strict mode
@ -829,31 +192,49 @@ scopes_tree_strict_mode (scopes_tree tree)
return (bool) tree->strict_mode;
}
/**
* Get number of subscopes (immediate function declarations / expressions) of the specified scope
*
* @return the number
*/
size_t
scopes_tree_child_scopes_num (scopes_tree tree) /**< a scopes tree node */
{
return tree->child_scopes_num;
} /* scopes_tree_child_scopes_num */
void
scopes_tree_init (void)
{
scopes_tree_root_node_p = NULL;
scopes_tree_last_node_p = NULL;
} /* scopes_tree_init */
void
scopes_tree_finalize (void)
{
JERRY_ASSERT (scopes_tree_root_node_p == NULL);
JERRY_ASSERT (scopes_tree_last_node_p == NULL);
} /* scopes_tree_finalize */
/**
* Initialize a scope
*
* @return initialized scope
*/
scopes_tree
scopes_tree_init (scopes_tree parent, /**< parent scope */
scope_type_t type) /**< scope type */
scopes_tree_new_scope (scopes_tree parent, /**< parent scope */
scope_type_t type) /**< scope type */
{
scopes_tree tree = (scopes_tree) jsp_mm_alloc (sizeof (scopes_tree_int));
memset (tree, 0, sizeof (scopes_tree_int));
tree->t.parent = (tree_header *) parent;
tree->t.children = null_list;
tree->t.children_num = 0;
if (parent != NULL)
{
if (parent->t.children_num == 0)
{
parent->t.children = linked_list_init (sizeof (scopes_tree));
}
linked_list_set_element (parent->t.children, parent->t.children_num, &tree);
void *added = linked_list_element (parent->t.children, parent->t.children_num);
JERRY_ASSERT (*(scopes_tree *) added == tree);
parent->t.children_num++;
}
tree->child_scopes_num = 0;
tree->child_scopes_processed_num = 0;
tree->max_uniq_literals_num = 0;
tree->bc_header_cp = MEM_CP_NULL;
tree->next_scope_cp = MEM_CP_NULL;
tree->instrs_count = 0;
tree->type = type;
tree->strict_mode = false;
@ -863,24 +244,53 @@ scopes_tree_init (scopes_tree parent, /**< parent scope */
tree->contains_try = false;
tree->contains_delete = false;
tree->contains_functions = false;
tree->instrs = linked_list_init (sizeof (op_meta));
tree->is_vars_and_args_to_regs_possible = false;
tree->var_decls = linked_list_init (sizeof (op_meta));
if (parent != NULL)
{
JERRY_ASSERT (scopes_tree_root_node_p != NULL);
JERRY_ASSERT (scopes_tree_last_node_p != NULL);
parent->child_scopes_num++;
}
else
{
JERRY_ASSERT (scopes_tree_root_node_p == NULL);
JERRY_ASSERT (scopes_tree_last_node_p == NULL);
scopes_tree_root_node_p = tree;
scopes_tree_last_node_p = tree;
}
MEM_CP_SET_NON_NULL_POINTER (scopes_tree_last_node_p->next_scope_cp, tree);
tree->next_scope_cp = MEM_CP_NULL;
scopes_tree_last_node_p = tree;
return tree;
} /* scopes_tree_init */
} /* scopes_tree_new_scope */
void
scopes_tree_free (scopes_tree tree)
scopes_tree_finish_build (void)
{
assert_tree (tree);
if (tree->t.children_num != 0)
{
for (uint8_t i = 0; i < tree->t.children_num; ++i)
{
scopes_tree_free (*(scopes_tree *) linked_list_element (tree->t.children, i));
}
linked_list_free (tree->t.children);
}
linked_list_free (tree->instrs);
linked_list_free (tree->var_decls);
jsp_mm_free (tree);
}
JERRY_ASSERT (scopes_tree_root_node_p != NULL);
JERRY_ASSERT (scopes_tree_last_node_p != NULL);
scopes_tree_root_node_p = NULL;
scopes_tree_last_node_p = NULL;
} /* scopes_tree_finish_build */
void
scopes_tree_free_scope (scopes_tree scope_p)
{
assert_tree (scope_p);
JERRY_ASSERT (scopes_tree_root_node_p == NULL);
JERRY_ASSERT (scopes_tree_last_node_p == NULL);
linked_list_free (scope_p->var_decls);
jsp_mm_free (scope_p);
} /* scopes_tree_free_scope */

View File

@ -19,7 +19,6 @@
#include "linked-list.h"
#include "lexer.h"
#include "ecma-globals.h"
#include "hash-table.h"
#include "opcodes.h"
#include "lit-id-hash-table.h"
#include "lit-literal.h"
@ -39,15 +38,13 @@ typedef struct
typedef struct tree_header
{
struct tree_header *parent;
linked_list children;
uint8_t children_num;
} tree_header;
/**
* Scope type
*/
typedef enum
typedef enum __attr_packed___
{
SCOPE_TYPE_GLOBAL, /**< the Global code scope */
SCOPE_TYPE_FUNCTION, /**< a function code scope */
@ -59,16 +56,27 @@ typedef enum
*/
typedef struct
{
tree_header t; /**< header */
linked_list instrs; /**< instructions */
mem_cpointer_t next_scope_cp; /**< next scope with same parent */
mem_cpointer_t bc_header_cp; /**< pointer to corresponding byte-code header
* (after bc_dump_single_scope) */
uint16_t child_scopes_num; /**< number of child scopes */
uint16_t child_scopes_processed_num; /**< number of child scopes, for which
* byte-code headers were already constructed */
uint16_t max_uniq_literals_num; /**< upper estimate number of entries
* in idx-literal hash table */
vm_instr_counter_t instrs_count; /**< count of instructions */
linked_list var_decls; /**< instructions for variable declarations */
scope_type_t type : 2; /**< scope type */
bool strict_mode: 1; /**< flag, indicating that scope's code should be executed in strict mode */
bool ref_arguments: 1; /**< flag, indicating that "arguments" variable is used inside the scope
* (not depends on subscopes) */
bool ref_eval: 1; /**< flag, indicating that "eval" is used inside the scope
* (not depends on subscopes) */
* (not depends on subscopes) */
bool contains_with: 1; /**< flag, indicationg whether 'with' statement is contained in the scope
* (not depends on subscopes) */
bool contains_try: 1; /**< flag, indicationg whether 'try' statement is contained in the scope
@ -76,25 +84,23 @@ typedef struct
bool contains_delete: 1; /**< flag, indicationg whether 'delete' operator is contained in the scope
* (not depends on subscopes) */
bool contains_functions: 1; /**< flag, indicating that the scope contains a function declaration / expression */
bool is_vars_and_args_to_regs_possible : 1; /**< the function's variables / arguments can be moved to registers */
} scopes_tree_int;
typedef scopes_tree_int *scopes_tree;
scopes_tree scopes_tree_init (scopes_tree, scope_type_t);
void scopes_tree_free (scopes_tree);
void scopes_tree_init (void);
void scopes_tree_finalize (void);
scopes_tree scopes_tree_new_scope (scopes_tree, scope_type_t);
void scopes_tree_free_scope (scopes_tree);
void scopes_tree_finish_build (void);
size_t scopes_tree_child_scopes_num (scopes_tree);
vm_instr_counter_t scopes_tree_instrs_num (scopes_tree);
vm_instr_counter_t scopes_tree_var_decls_num (scopes_tree);
void scopes_tree_add_op_meta (scopes_tree, op_meta);
void scopes_tree_add_var_decl (scopes_tree, op_meta);
void scopes_tree_set_op_meta (scopes_tree, vm_instr_counter_t, op_meta);
void scopes_tree_set_instrs_num (scopes_tree, vm_instr_counter_t);
op_meta scopes_tree_op_meta (scopes_tree, vm_instr_counter_t);
op_meta scopes_tree_var_decl (scopes_tree, vm_instr_counter_t);
void scopes_tree_remove_op_meta (scopes_tree tree, vm_instr_counter_t oc);
size_t scopes_tree_count_literals_in_blocks (scopes_tree);
vm_instr_counter_t scopes_tree_count_instructions (scopes_tree);
bool scopes_tree_variable_declaration_exists (scopes_tree, lit_cpointer_t);
vm_instr_t *scopes_tree_raw_data (scopes_tree, uint8_t *, size_t, lit_id_hash_table *);
void scopes_tree_dump_var_decls (scopes_tree, lit_cpointer_t *);
void scopes_tree_set_strict_mode (scopes_tree, bool);
void scopes_tree_set_arguments_used (scopes_tree);
void scopes_tree_set_eval_used (scopes_tree);
@ -103,5 +109,4 @@ void scopes_tree_set_contains_try (scopes_tree);
void scopes_tree_set_contains_delete (scopes_tree);
void scopes_tree_set_contains_functions (scopes_tree);
bool scopes_tree_strict_mode (scopes_tree);
#endif /* SCOPES_TREE_H */

View File

@ -1,476 +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.
*/
#include "lit-id-hash-table.h"
#include "serializer.h"
#include "bytecode-data.h"
#include "pretty-printer.h"
#include "array-list.h"
#include "scopes-tree.h"
static bytecode_data_header_t *first_bytecode_header_p;
static scopes_tree current_scope;
static bool print_instrs;
static void
serializer_print_instrs (const bytecode_data_header_t *);
op_meta
serializer_get_op_meta (vm_instr_counter_t oc)
{
JERRY_ASSERT (current_scope);
return scopes_tree_op_meta (current_scope, oc);
}
/**
* Get byte-code instruction from current scope, or specified byte-code array
*
* @return byte-code instruction
*/
vm_instr_t
serializer_get_instr (const bytecode_data_header_t *bytecode_data_p, /**< pointer to byte-code data (or NULL,
* if instruction should be taken from
* instruction list of current scope) */
vm_instr_counter_t oc) /**< position of the intruction */
{
if (bytecode_data_p == NULL)
{
return serializer_get_op_meta (oc).op;
}
else
{
JERRY_ASSERT (oc < bytecode_data_p->instrs_count);
return bytecode_data_p->instrs_p[oc];
}
} /* serializer_get_instr */
/**
* Convert literal id (operand value of instruction) to compressed pointer to literal
*
* Bytecode is divided into blocks of fixed size and each block has independent encoding of variable names,
* which are represented by 8 bit numbers - ids.
* This function performs conversion from id to literal.
*
* @return compressed pointer to literal
*/
lit_cpointer_t
serializer_get_literal_cp_by_uid (uint8_t id, /**< literal idx */
const bytecode_data_header_t *bytecode_data_p, /**< pointer to bytecode */
vm_instr_counter_t oc) /**< position in the bytecode */
{
lit_id_hash_table *lit_id_hash = null_hash;
if (bytecode_data_p)
{
lit_id_hash = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
}
else
{
lit_id_hash = MEM_CP_GET_POINTER (lit_id_hash_table, first_bytecode_header_p->lit_id_hash_cp);
}
if (lit_id_hash == null_hash)
{
return INVALID_LITERAL;
}
return lit_id_hash_table_lookup (lit_id_hash, id, oc);
} /* serializer_get_literal_cp_by_uid */
void
serializer_set_scope (scopes_tree new_scope)
{
current_scope = new_scope;
}
/**
* Dump scope to current scope
*
* NOTE:
* This function is used for processing of function expressions as they should not be hoisted.
* After parsing a function expression, it is immediately dumped to current scope via call of this function.
*/
void
serializer_dump_subscope (scopes_tree tree) /**< scope to dump */
{
JERRY_ASSERT (tree != NULL);
vm_instr_counter_t instr_pos;
bool header = true;
for (instr_pos = 0; instr_pos < tree->instrs_count; instr_pos++)
{
op_meta *om_p = (op_meta *) linked_list_element (tree->instrs, instr_pos);
if (om_p->op.op_idx != VM_OP_VAR_DECL
&& om_p->op.op_idx != VM_OP_META && !header)
{
break;
}
if (om_p->op.op_idx == VM_OP_REG_VAR_DECL)
{
header = false;
}
scopes_tree_add_op_meta (current_scope, *om_p);
}
for (vm_instr_counter_t var_decl_pos = 0;
var_decl_pos < linked_list_get_length (tree->var_decls);
var_decl_pos++)
{
op_meta *om_p = (op_meta *) linked_list_element (tree->var_decls, var_decl_pos);
scopes_tree_add_op_meta (current_scope, *om_p);
}
for (uint8_t child_id = 0; child_id < tree->t.children_num; child_id++)
{
serializer_dump_subscope (*(scopes_tree *) linked_list_element (tree->t.children, child_id));
}
for (; instr_pos < tree->instrs_count; instr_pos++)
{
op_meta *om_p = (op_meta *) linked_list_element (tree->instrs, instr_pos);
scopes_tree_add_op_meta (current_scope, *om_p);
}
} /* serializer_dump_subscope */
/**
* Merge scopes tree into bytecode
*
* @return pointer to generated bytecode
*/
const bytecode_data_header_t *
serializer_merge_scopes_into_bytecode (void)
{
const size_t buckets_count = scopes_tree_count_literals_in_blocks (current_scope);
const vm_instr_counter_t instrs_count = scopes_tree_count_instructions (current_scope);
const size_t blocks_count = JERRY_ALIGNUP (instrs_count, BLOCK_SIZE) / BLOCK_SIZE;
const size_t bytecode_size = JERRY_ALIGNUP (instrs_count * sizeof (vm_instr_t), MEM_ALIGNMENT);
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (buckets_count, blocks_count);
const size_t header_and_hash_table_size = JERRY_ALIGNUP (sizeof (bytecode_data_header_t) + hash_table_size,
MEM_ALIGNMENT);
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (bytecode_size + header_and_hash_table_size,
MEM_HEAP_ALLOC_LONG_TERM);
lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (buffer_p + sizeof (bytecode_data_header_t),
hash_table_size,
buckets_count, blocks_count);
vm_instr_t *bytecode_p = scopes_tree_raw_data (current_scope,
buffer_p + header_and_hash_table_size,
bytecode_size,
lit_id_hash);
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
MEM_CP_SET_POINTER (header_p->lit_id_hash_cp, lit_id_hash);
header_p->instrs_p = bytecode_p;
header_p->instrs_count = instrs_count;
MEM_CP_SET_POINTER (header_p->next_header_cp, first_bytecode_header_p);
first_bytecode_header_p = header_p;
if (print_instrs)
{
lit_dump_literals ();
serializer_print_instrs (header_p);
}
return header_p;
} /* serializer_merge_scopes_into_bytecode */
void
serializer_dump_op_meta (op_meta op)
{
JERRY_ASSERT (scopes_tree_instrs_num (current_scope) < MAX_OPCODES);
scopes_tree_add_op_meta (current_scope, op);
#ifdef JERRY_ENABLE_PRETTY_PRINTER
if (print_instrs)
{
pp_op_meta (NULL, (vm_instr_counter_t) (scopes_tree_instrs_num (current_scope) - 1), op, false);
}
#endif
}
/**
* Dump variable declaration into the current scope
*/
void
serializer_dump_var_decl (op_meta op) /**< variable declaration instruction */
{
JERRY_ASSERT (scopes_tree_instrs_num (current_scope)
+ linked_list_get_length (current_scope->var_decls) < MAX_OPCODES);
scopes_tree_add_var_decl (current_scope, op);
} /* serializer_dump_var_decl */
vm_instr_counter_t
serializer_get_current_instr_counter (void)
{
return scopes_tree_instrs_num (current_scope);
}
vm_instr_counter_t
serializer_count_instrs_in_subscopes (void)
{
return (vm_instr_counter_t) (scopes_tree_count_instructions (current_scope) - scopes_tree_instrs_num (current_scope));
}
void
serializer_set_writing_position (vm_instr_counter_t oc)
{
scopes_tree_set_instrs_num (current_scope, oc);
}
void
serializer_rewrite_op_meta (const vm_instr_counter_t loc, op_meta op)
{
scopes_tree_set_op_meta (current_scope, loc, op);
#ifdef JERRY_ENABLE_PRETTY_PRINTER
if (print_instrs)
{
pp_op_meta (NULL, loc, op, true);
}
#endif
}
static void
serializer_print_instrs (const bytecode_data_header_t *bytecode_data_p)
{
#ifdef JERRY_ENABLE_PRETTY_PRINTER
for (vm_instr_counter_t loc = 0; loc < bytecode_data_p->instrs_count; loc++)
{
op_meta opm;
opm.op = bytecode_data_p->instrs_p[loc];
for (int i = 0; i < 3; i++)
{
opm.lit_id[i] = NOT_A_LITERAL;
}
pp_op_meta (bytecode_data_p, loc, opm, false);
}
#else
(void) bytecode_data_p;
#endif
}
void
serializer_init ()
{
current_scope = NULL;
print_instrs = false;
first_bytecode_header_p = NULL;
lit_init ();
}
void serializer_set_show_instrs (bool show_instrs)
{
print_instrs = show_instrs;
}
/**
* Deletes bytecode and associated hash table
*/
void
serializer_remove_bytecode_data (const bytecode_data_header_t *bytecode_data_p) /**< pointer to bytecode data which
* should be deleted */
{
bytecode_data_header_t *prev_header = NULL;
bytecode_data_header_t *cur_header_p = first_bytecode_header_p;
while (cur_header_p != NULL)
{
if (cur_header_p == bytecode_data_p)
{
if (prev_header)
{
prev_header->next_header_cp = cur_header_p->next_header_cp;
}
else
{
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
}
mem_heap_free_block (cur_header_p);
break;
}
prev_header = cur_header_p;
cur_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, cur_header_p->next_header_cp);
}
} /* serializer_remove_instructions */
void
serializer_free (void)
{
lit_finalize ();
while (first_bytecode_header_p != NULL)
{
bytecode_data_header_t *header_p = first_bytecode_header_p;
first_bytecode_header_p = MEM_CP_GET_POINTER (bytecode_data_header_t, header_p->next_header_cp);
mem_heap_free_block (header_p);
}
}
#ifdef JERRY_ENABLE_SNAPSHOT
/**
* Dump byte-code and idx-to-literal map to snapshot
*
* @return true, upon success (i.e. buffer size is enough),
* false - otherwise.
*/
bool
serializer_dump_bytecode_with_idx_map (uint8_t *buffer_p, /**< buffer to dump to */
size_t buffer_size, /**< buffer size */
size_t *in_out_buffer_offset_p, /**< in-out: buffer write offset */
const bytecode_data_header_t *bytecode_data_p, /**< byte-code data */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map from literal
* identifiers in
* literal storage
* to literal offsets
* in snapshot */
uint32_t literals_num, /**< literals number */
uint32_t *out_bytecode_size_p, /**< out: size of dumped instructions array */
uint32_t *out_idx_to_lit_map_size_p) /**< out: side of dumped
* idx to literals map */
{
JERRY_ASSERT (bytecode_data_p->next_header_cp == MEM_CP_NULL);
vm_instr_counter_t instrs_num = bytecode_data_p->instrs_count;
const size_t instrs_array_size = sizeof (vm_instr_t) * instrs_num;
if (*in_out_buffer_offset_p + instrs_array_size > buffer_size)
{
return false;
}
memcpy (buffer_p + *in_out_buffer_offset_p, bytecode_data_p->instrs_p, instrs_array_size);
*in_out_buffer_offset_p += instrs_array_size;
*out_bytecode_size_p = (uint32_t) (sizeof (vm_instr_t) * instrs_num);
lit_id_hash_table *lit_id_hash_p = MEM_CP_GET_POINTER (lit_id_hash_table, bytecode_data_p->lit_id_hash_cp);
uint32_t idx_to_lit_map_size = lit_id_hash_table_dump_for_snapshot (buffer_p,
buffer_size,
in_out_buffer_offset_p,
lit_id_hash_p,
lit_map_p,
literals_num,
instrs_num);
if (idx_to_lit_map_size == 0)
{
return false;
}
else
{
*out_idx_to_lit_map_size_p = idx_to_lit_map_size;
return true;
}
} /* serializer_dump_bytecode_with_idx_map */
/**
* Register bytecode and idx map from snapshot
*
* NOTE:
* If is_copy flag is set, bytecode is copied from snapshot, else bytecode is referenced directly
* from snapshot
*
* @return pointer to byte-code header, upon success,
* NULL - upon failure (i.e., in case snapshot format is not valid)
*/
const bytecode_data_header_t *
serializer_load_bytecode_with_idx_map (const uint8_t *bytecode_and_idx_map_p, /**< buffer with instructions array
* and idx to literals map from
* snapshot */
uint32_t bytecode_size, /**< size of instructions array */
uint32_t idx_to_lit_map_size, /**< size of the idx to literals map */
const lit_mem_to_snapshot_id_map_entry_t *lit_map_p, /**< map of in-snapshot
* literal offsets
* to literal identifiers,
* created in literal
* storage */
uint32_t literals_num, /**< number of literals */
bool is_copy) /** flag, indicating whether the passed in-snapshot data
* should be copied to engine's memory (true),
* or it can be referenced until engine is stopped
* (i.e. until call to jerry_cleanup) */
{
const uint8_t *idx_to_lit_map_p = bytecode_and_idx_map_p + bytecode_size;
size_t instructions_number = bytecode_size / sizeof (vm_instr_t);
size_t blocks_count = JERRY_ALIGNUP (instructions_number, BLOCK_SIZE) / BLOCK_SIZE;
uint32_t idx_num_total;
size_t idx_to_lit_map_offset = 0;
if (!jrt_read_from_buffer_by_offset (idx_to_lit_map_p,
idx_to_lit_map_size,
&idx_to_lit_map_offset,
&idx_num_total))
{
return NULL;
}
const size_t bytecode_alloc_size = JERRY_ALIGNUP (bytecode_size, MEM_ALIGNMENT);
const size_t hash_table_size = lit_id_hash_table_get_size_for_table (idx_num_total, blocks_count);
const size_t header_and_hash_table_size = JERRY_ALIGNUP (sizeof (bytecode_data_header_t) + hash_table_size,
MEM_ALIGNMENT);
const size_t alloc_size = header_and_hash_table_size + (is_copy ? bytecode_alloc_size : 0);
uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (alloc_size, MEM_HEAP_ALLOC_LONG_TERM);
bytecode_data_header_t *header_p = (bytecode_data_header_t *) buffer_p;
vm_instr_t *instrs_p;
vm_instr_t *snapshot_instrs_p = (vm_instr_t *) bytecode_and_idx_map_p;
if (is_copy)
{
instrs_p = (vm_instr_t *) (buffer_p + header_and_hash_table_size);
memcpy (instrs_p, snapshot_instrs_p, bytecode_size);
}
else
{
instrs_p = snapshot_instrs_p;
}
uint8_t *lit_id_hash_table_buffer_p = buffer_p + sizeof (bytecode_data_header_t);
if (lit_id_hash_table_load_from_snapshot (blocks_count,
idx_num_total,
idx_to_lit_map_p + idx_to_lit_map_offset,
idx_to_lit_map_size - idx_to_lit_map_offset,
lit_map_p,
literals_num,
lit_id_hash_table_buffer_p,
hash_table_size)
&& (vm_instr_counter_t) instructions_number == instructions_number)
{
MEM_CP_SET_NON_NULL_POINTER (header_p->lit_id_hash_cp, lit_id_hash_table_buffer_p);
header_p->instrs_p = instrs_p;
header_p->instrs_count = (vm_instr_counter_t) instructions_number;
MEM_CP_SET_POINTER (header_p->next_header_cp, first_bytecode_header_p);
first_bytecode_header_p = header_p;
return header_p;
}
else
{
mem_heap_free_block (buffer_p);
return NULL;
}
} /* serializer_load_bytecode_with_idx_map */
#endif /* JERRY_ENABLE_SNAPSHOT */

View File

@ -1,58 +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 SERIALIZER_H
#define SERIALIZER_H
#include "bytecode-data.h"
#include "jrt.h"
#include "ecma-globals.h"
#include "opcodes.h"
#include "vm.h"
#include "scopes-tree.h"
#define NOT_A_LITERAL (lit_cpointer_t::null_cp ())
void serializer_init ();
void serializer_set_show_instrs (bool);
op_meta serializer_get_op_meta (vm_instr_counter_t);
vm_instr_t serializer_get_instr (const bytecode_data_header_t *, vm_instr_counter_t);
lit_cpointer_t serializer_get_literal_cp_by_uid (uint8_t, const bytecode_data_header_t *, vm_instr_counter_t);
void serializer_set_scope (scopes_tree);
void serializer_dump_subscope (scopes_tree);
const bytecode_data_header_t *serializer_merge_scopes_into_bytecode (void);
void serializer_dump_op_meta (op_meta);
void serializer_dump_var_decl (op_meta);
vm_instr_counter_t serializer_get_current_instr_counter (void);
vm_instr_counter_t serializer_count_instrs_in_subscopes (void);
void serializer_set_writing_position (vm_instr_counter_t);
void serializer_rewrite_op_meta (vm_instr_counter_t, op_meta);
void serializer_remove_bytecode_data (const bytecode_data_header_t *);
void serializer_free (void);
#ifdef JERRY_ENABLE_SNAPSHOT
/*
* Snapshot-related
*/
bool serializer_dump_bytecode_with_idx_map (uint8_t *, size_t, size_t *, const bytecode_data_header_t *,
const lit_mem_to_snapshot_id_map_entry_t *, uint32_t,
uint32_t *, uint32_t *);
const bytecode_data_header_t *
serializer_load_bytecode_with_idx_map (const uint8_t *, uint32_t, uint32_t,
const lit_mem_to_snapshot_id_map_entry_t *, uint32_t, bool);
#endif /* JERRY_ENABLE_SNAPSHOT */
#endif /* SERIALIZER_H */

View File

@ -32,11 +32,17 @@
#include "ecma-reference.h"
#include "ecma-regexp-object.h"
#include "ecma-try-catch-macro.h"
#include "serializer.h"
bool vm_is_reg_variable (vm_idx_t);
ecma_completion_value_t get_variable_value (vm_frame_ctx_t *, vm_idx_t, bool);
ecma_completion_value_t set_variable_value (vm_frame_ctx_t *, vm_instr_counter_t, vm_idx_t, ecma_value_t);
ecma_completion_value_t vm_fill_varg_list (vm_frame_ctx_t *, ecma_length_t, ecma_collection_header_t *);
extern void vm_fill_params_list (vm_frame_ctx_t *, ecma_length_t, ecma_collection_header_t *);
extern vm_instr_counter_t vm_fill_params_list (const bytecode_data_header_t *,
vm_instr_counter_t,
ecma_length_t,
ecma_collection_header_t *);
extern ecma_completion_value_t vm_function_declaration (const bytecode_data_header_t *bytecode_header_p,
bool is_strict,
bool is_eval_code,
ecma_object_t *lex_env_p);
#endif /* OPCODES_ECMA_SUPPORT_H */

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "jrt.h"
#include "vm.h"
#include "opcodes.h"
@ -49,7 +50,9 @@ opfunc_try_block (vm_instr_t instr, /**< instruction */
if (next_instr.data.meta.type == OPCODE_META_TYPE_CATCH)
{
const vm_instr_counter_t catch_end_oc = (vm_instr_counter_t) (
vm_read_instr_counter_from_meta (OPCODE_META_TYPE_CATCH, frame_ctx_p) + frame_ctx_p->pos);
vm_read_instr_counter_from_meta (OPCODE_META_TYPE_CATCH,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos) + frame_ctx_p->pos);
frame_ctx_p->pos++;
if (ecma_is_completion_value_throw (try_completion))
@ -58,9 +61,9 @@ opfunc_try_block (vm_instr_t instr, /**< instruction */
JERRY_ASSERT (next_instr.op_idx == VM_OP_META);
JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER);
lit_cpointer_t catch_exc_val_var_name_lit_cp = serializer_get_literal_cp_by_uid (next_instr.data.meta.data_1,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t catch_exc_val_var_name_lit_cp = bc_get_literal_cp_by_uid (next_instr.data.meta.data_1,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
frame_ctx_p->pos++;
ecma_string_t *catch_exc_var_name_str_p = ecma_new_ecma_string_from_lit_cp (catch_exc_val_var_name_lit_cp);
@ -104,7 +107,9 @@ opfunc_try_block (vm_instr_t instr, /**< instruction */
if (next_instr.data.meta.type == OPCODE_META_TYPE_FINALLY)
{
const vm_instr_counter_t finally_end_oc = (vm_instr_counter_t) (
vm_read_instr_counter_from_meta (OPCODE_META_TYPE_FINALLY, frame_ctx_p) + frame_ctx_p->pos);
vm_read_instr_counter_from_meta (OPCODE_META_TYPE_FINALLY,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos) + frame_ctx_p->pos);
frame_ctx_p->pos++;
vm_run_scope_t run_scope_finally = { frame_ctx_p->pos, finally_end_oc };

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "jrt.h"
#include "opcodes.h"
#include "opcodes-ecma-support.h"

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "opcodes-ecma-support.h"
#ifndef JERRY_NDEBUG
@ -89,9 +90,9 @@ get_variable_value (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
else
{
ecma_string_t var_name_string;
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (var_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (var_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
ecma_new_ecma_string_on_stack_from_lit_cp (&var_name_string, lit_cp);
@ -155,7 +156,7 @@ set_variable_value (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
else
{
ecma_string_t var_name_string;
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (var_idx, frame_ctx_p->bytecode_header_p, lit_oc);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (var_idx, frame_ctx_p->bytecode_header_p, lit_oc);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
ecma_new_ecma_string_on_stack_from_lit_cp (&var_name_string, lit_cp);

View File

@ -13,11 +13,12 @@
* limitations under the License.
*/
#include "opcodes-ecma-support.h"
#include "bytecode-data.h"
#include "jrt.h"
#include "vm.h"
#include "opcodes.h"
#include "opcodes-ecma-support.h"
#include "vm.h"
/**
* Fill arguments' list
@ -69,25 +70,28 @@ vm_fill_varg_list (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
/**
* Fill parameters' list
*/
void
vm_fill_params_list (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
vm_instr_counter_t
vm_fill_params_list (const bytecode_data_header_t *bytecode_header_p, /**< header of byte-code */
vm_instr_counter_t first_instr_pos, /**< position of the first instruction
* with a formal parameter's name */
ecma_length_t params_number, /**< number of parameters */
ecma_collection_header_t *formal_params_collection_p) /**< collection to fill with
* parameters' names */
{
vm_instr_counter_t instr_pos = first_instr_pos;
uint32_t param_index;
for (param_index = 0;
param_index < params_number;
param_index++)
{
vm_instr_t next_instr = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos);
vm_instr_t next_instr = vm_get_instr (bytecode_header_p->instrs_p, instr_pos);
JERRY_ASSERT (next_instr.op_idx == VM_OP_META);
JERRY_ASSERT (next_instr.data.meta.type == OPCODE_META_TYPE_VARG);
const lit_cpointer_t param_name_lit_idx = serializer_get_literal_cp_by_uid (next_instr.data.meta.data_1,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
const lit_cpointer_t param_name_lit_idx = bc_get_literal_cp_by_uid (next_instr.data.meta.data_1,
bytecode_header_p,
instr_pos);
ecma_string_t *param_name_str_p = ecma_new_ecma_string_from_lit_cp (param_name_lit_idx);
ecma_value_t param_name_value = ecma_make_string_value (param_name_str_p);
@ -96,8 +100,10 @@ vm_fill_params_list (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
ecma_deref_ecma_string (param_name_str_p);
frame_ctx_p->pos++;
instr_pos++;
}
JERRY_ASSERT (param_index == params_number);
return instr_pos;
} /* vm_fill_params_list */

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "jrt.h"
#include "opcodes.h"
#include "opcodes-ecma-support.h"
@ -77,9 +78,9 @@ opfunc_assignment (vm_instr_t instr, /**< instruction */
}
else if (type_value_right == OPCODE_ARG_TYPE_STRING)
{
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
ecma_string_t *string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
ret_value = set_variable_value (frame_ctx_p,
@ -108,9 +109,9 @@ opfunc_assignment (vm_instr_t instr, /**< instruction */
{
ecma_number_t *num_p = frame_ctx_p->tmp_num_p;
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
literal_t lit = lit_get_literal_by_cp (lit_cp);
JERRY_ASSERT (lit->get_type () == LIT_NUMBER_T);
@ -125,9 +126,9 @@ opfunc_assignment (vm_instr_t instr, /**< instruction */
{
ecma_number_t *num_p = frame_ctx_p->tmp_num_p;
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
literal_t lit = lit_get_literal_by_cp (lit_cp);
JERRY_ASSERT (lit->get_type () == LIT_NUMBER_T);
@ -152,7 +153,7 @@ opfunc_assignment (vm_instr_t instr, /**< instruction */
else if (type_value_right == OPCODE_ARG_TYPE_REGEXP)
{
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (src_val_descr,
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (src_val_descr,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
ecma_string_t *string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
@ -440,40 +441,10 @@ opfunc_reg_var_decl (vm_instr_t instr __attr_unused___, /**< instruction */
* However, ecma_free_completion_value may be called for it, but it is a no-op.
*/
ecma_completion_value_t
opfunc_var_decl (vm_instr_t instr, /**< instruction */
vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */
opfunc_var_decl (vm_instr_t instr __attr_unused___, /**< instruction */
vm_frame_ctx_t *frame_ctx_p __attr_unused___) /**< interpreter context */
{
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (instr.data.var_decl.variable_name,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
ecma_string_t *var_name_string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
if (!ecma_op_has_binding (frame_ctx_p->lex_env_p, var_name_string_p))
{
const bool is_configurable_bindings = frame_ctx_p->is_eval_code;
ecma_completion_value_t completion = ecma_op_create_mutable_binding (frame_ctx_p->lex_env_p,
var_name_string_p,
is_configurable_bindings);
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
/* Skipping SetMutableBinding as we have already checked that there were not
* any binding with specified name in current lexical environment
* and CreateMutableBinding sets the created binding's value to undefined */
JERRY_ASSERT (ecma_is_completion_value_normal_simple_value (ecma_op_get_binding_value (frame_ctx_p->lex_env_p,
var_name_string_p,
true),
ECMA_SIMPLE_VALUE_UNDEFINED));
}
ecma_deref_ecma_string (var_name_string_p);
frame_ctx_p->pos++;
return ecma_make_empty_completion_value ();
JERRY_UNREACHABLE ();
} /* opfunc_var_decl */
/**
@ -482,74 +453,156 @@ opfunc_var_decl (vm_instr_t instr, /**< instruction */
* @return completion value
* returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
function_declaration (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context */
lit_cpointer_t function_name_lit_cp, /**< compressed pointer to literal with function name */
ecma_collection_header_t *formal_params_collection_p) /** formal parameters collection */
ecma_completion_value_t
vm_function_declaration (const bytecode_data_header_t *bytecode_header_p, /**< byte-code header */
bool is_strict, /**< is function declared in strict mode code? */
bool is_eval_code, /**< is function declared in eval code? */
ecma_object_t *lex_env_p) /**< lexical environment to use */
{
const bool is_configurable_bindings = frame_ctx_p->is_eval_code;
vm_instr_t *function_instrs_p = bytecode_header_p->instrs_p;
const vm_instr_counter_t function_code_end_oc = (vm_instr_counter_t) (
vm_read_instr_counter_from_meta (OPCODE_META_TYPE_FUNCTION_END, frame_ctx_p) + frame_ctx_p->pos);
frame_ctx_p->pos++;
vm_instr_counter_t instr_pos = 0;
ecma_string_t *function_name_string_p = ecma_new_ecma_string_from_lit_cp (function_name_lit_cp);
vm_instr_t func_header_instr = function_instrs_p[instr_pos];
ecma_completion_value_t ret_value = ecma_op_function_declaration (frame_ctx_p->lex_env_p,
function_name_string_p,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos,
formal_params_collection_p,
frame_ctx_p->is_strict,
is_configurable_bindings);
ecma_deref_ecma_string (function_name_string_p);
bool is_func_decl;
vm_idx_t function_name_idx;
ecma_length_t params_number;
frame_ctx_p->pos = function_code_end_oc;
if (func_header_instr.op_idx == VM_OP_FUNC_DECL_N)
{
is_func_decl = true;
function_name_idx = func_header_instr.data.func_decl_n.name_lit_idx;
params_number = func_header_instr.data.func_decl_n.arg_list;
}
else
{
is_func_decl = false;
function_name_idx = func_header_instr.data.func_expr_n.name_lit_idx;
params_number = func_header_instr.data.func_expr_n.arg_list;
}
lit_cpointer_t function_name_lit_cp = NOT_A_LITERAL;
if (function_name_idx != VM_IDX_EMPTY)
{
function_name_lit_cp = bc_get_literal_cp_by_uid (function_name_idx,
bytecode_header_p,
instr_pos);
}
else
{
JERRY_ASSERT (!is_func_decl);
}
instr_pos++;
ecma_completion_value_t ret_value;
ecma_collection_header_t *formal_params_collection_p = NULL;
if (bytecode_header_p->is_args_moved_to_regs)
{
instr_pos = (vm_instr_counter_t) (instr_pos + params_number);
}
else if (params_number != 0)
{
formal_params_collection_p = ecma_new_strings_collection (NULL, 0);
instr_pos = vm_fill_params_list (bytecode_header_p,
instr_pos,
params_number,
formal_params_collection_p);
}
if (is_func_decl)
{
const bool is_configurable_bindings = is_eval_code;
ecma_string_t *function_name_string_p = ecma_new_ecma_string_from_lit_cp (function_name_lit_cp);
ret_value = ecma_op_function_declaration (lex_env_p,
function_name_string_p,
bytecode_header_p,
instr_pos,
formal_params_collection_p,
is_strict,
is_configurable_bindings);
ecma_deref_ecma_string (function_name_string_p);
}
else
{
ecma_object_t *scope_p;
ecma_string_t *function_name_string_p = NULL;
bool is_named_func_expr = (function_name_idx != VM_IDX_EMPTY);
if (is_named_func_expr)
{
scope_p = ecma_create_decl_lex_env (lex_env_p);
JERRY_ASSERT (function_name_lit_cp.packed_value != MEM_CP_NULL);
function_name_string_p = ecma_new_ecma_string_from_lit_cp (function_name_lit_cp);
ecma_op_create_immutable_binding (scope_p, function_name_string_p);
}
else
{
scope_p = lex_env_p;
ecma_ref_object (scope_p);
}
ecma_object_t *func_obj_p = ecma_op_create_function_object (formal_params_collection_p,
scope_p,
is_strict,
bytecode_header_p,
instr_pos);
ret_value = ecma_make_normal_completion_value (ecma_make_object_value (func_obj_p));
if (is_named_func_expr)
{
ecma_op_initialize_immutable_binding (scope_p,
function_name_string_p,
ecma_make_object_value (func_obj_p));
ecma_deref_ecma_string (function_name_string_p);
}
ecma_deref_object (scope_p);
}
return ret_value;
} /* function_declaration */
} /* vm_function_declaration */
/**
* 'Function declaration' opcode handler.
* 'Function declaration header' opcode handler.
*
* @return completion value
* returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
opfunc_func_decl_n (vm_instr_t instr, /**< instruction */
vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */
opfunc_func_decl_n (vm_instr_t instr __attr_unused___, /**< instruction */
vm_frame_ctx_t *frame_ctx_p __attr_unused___) /**< interpreter context */
{
const vm_idx_t function_name_idx = instr.data.func_decl_n.name_lit_idx;
const ecma_length_t params_number = instr.data.func_decl_n.arg_list;
lit_cpointer_t function_name_lit_cp = serializer_get_literal_cp_by_uid (function_name_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
frame_ctx_p->pos++;
ecma_completion_value_t ret_value;
ecma_collection_header_t *formal_params_collection_p;
if (params_number != 0)
{
formal_params_collection_p = ecma_new_strings_collection (NULL, 0);
vm_fill_params_list (frame_ctx_p, params_number, formal_params_collection_p);
}
else
{
formal_params_collection_p = NULL;
}
ret_value = function_declaration (frame_ctx_p,
function_name_lit_cp,
formal_params_collection_p);
return ret_value;
JERRY_UNREACHABLE ();
} /* opfunc_func_decl_n */
/**
* 'Function expression header' opcode handler.
*
* @return completion value
* returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
opfunc_func_expr_n (vm_instr_t instr __attr_unused___, /**< instruction */
vm_frame_ctx_t *frame_ctx_p __attr_unused___) /**< interpreter context */
{
JERRY_UNREACHABLE ();
} /* opfunc_func_expr_n */
/**
* 'Function expression' opcode handler.
*
@ -557,84 +610,46 @@ opfunc_func_decl_n (vm_instr_t instr, /**< instruction */
* returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
opfunc_func_expr_n (vm_instr_t instr, /**< instruction */
vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */
opfunc_func_expr_ref (vm_instr_t instr, /**< instruction */
vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */
{
const vm_instr_counter_t lit_oc = frame_ctx_p->pos;
frame_ctx_p->pos++;
const vm_idx_t dst_var_idx = instr.data.func_expr_n.lhs;
const vm_idx_t function_name_lit_idx = instr.data.func_expr_n.name_lit_idx;
const ecma_length_t params_number = instr.data.func_expr_n.arg_list;
const bool is_named_func_expr = (function_name_lit_idx != VM_IDX_EMPTY);
const vm_idx_t dst_var_idx = instr.data.func_expr_ref.lhs;
const vm_idx_t idx1 = instr.data.func_expr_ref.idx1;
const vm_idx_t idx2 = instr.data.func_expr_ref.idx2;
uint16_t index = (uint16_t) jrt_set_bit_field_value (jrt_set_bit_field_value (0, idx1, 0, JERRY_BITSINBYTE),
idx2, JERRY_BITSINBYTE, JERRY_BITSINBYTE);
const bytecode_data_header_t *header_p = frame_ctx_p->bytecode_header_p;
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t, header_p->declarations_cp);
JERRY_ASSERT (index < header_p->func_scopes_count);
const bytecode_data_header_t *func_expr_bc_header_p = MEM_CP_GET_NON_NULL_POINTER (const bytecode_data_header_t,
declarations_p[index]);
JERRY_ASSERT (func_expr_bc_header_p->instrs_p[0].op_idx == VM_OP_FUNC_EXPR_N);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
vm_instr_counter_t function_code_end_oc;
ECMA_TRY_CATCH (func_obj_value,
vm_function_declaration (func_expr_bc_header_p,
frame_ctx_p->is_strict,
frame_ctx_p->is_eval_code,
frame_ctx_p->lex_env_p),
ret_value);
ecma_collection_header_t *formal_params_collection_p;
JERRY_ASSERT (ecma_is_constructor (func_obj_value));
if (params_number != 0)
{
formal_params_collection_p = ecma_new_strings_collection (NULL, 0);
ret_value = set_variable_value (frame_ctx_p, lit_oc, dst_var_idx, func_obj_value);
vm_fill_params_list (frame_ctx_p, params_number, formal_params_collection_p);
}
else
{
formal_params_collection_p = NULL;
}
function_code_end_oc = (vm_instr_counter_t) (vm_read_instr_counter_from_meta (OPCODE_META_TYPE_FUNCTION_END,
frame_ctx_p) + frame_ctx_p->pos);
frame_ctx_p->pos++;
ecma_object_t *scope_p;
ecma_string_t *function_name_string_p = NULL;
if (is_named_func_expr)
{
scope_p = ecma_create_decl_lex_env (frame_ctx_p->lex_env_p);
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (function_name_lit_idx,
frame_ctx_p->bytecode_header_p,
lit_oc);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
function_name_string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
ecma_op_create_immutable_binding (scope_p, function_name_string_p);
}
else
{
scope_p = frame_ctx_p->lex_env_p;
ecma_ref_object (scope_p);
}
ecma_object_t *func_obj_p = ecma_op_create_function_object (formal_params_collection_p,
scope_p,
frame_ctx_p->is_strict,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
ret_value = set_variable_value (frame_ctx_p, lit_oc,
dst_var_idx,
ecma_make_object_value (func_obj_p));
if (is_named_func_expr)
{
ecma_op_initialize_immutable_binding (scope_p,
function_name_string_p,
ecma_make_object_value (func_obj_p));
ecma_deref_ecma_string (function_name_string_p);
}
ecma_deref_object (func_obj_p);
ecma_deref_object (scope_p);
frame_ctx_p->pos = function_code_end_oc;
ECMA_FINALIZE (func_obj_value);
return ret_value;
} /* opfunc_func_expr_n */
} /* opfunc_func_expr_ref */
/**
* Get 'this' argument value and call flags mask for function call
@ -718,9 +733,9 @@ vm_helper_call_get_call_flags_and_this_arg (vm_frame_ctx_t *int_data_p, /**< int
/* 6.b.i */
ecma_string_t var_name_string;
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (var_idx,
int_data_p->bytecode_header_p,
var_idx_lit_oc);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (var_idx,
int_data_p->bytecode_header_p,
var_idx_lit_oc);
ecma_new_ecma_string_on_stack_from_lit_cp (&var_name_string, lit_cp);
ecma_object_t *ref_base_lex_env_p = ecma_op_resolve_reference_base (int_data_p->lex_env_p,
@ -1038,9 +1053,9 @@ opfunc_obj_decl (vm_instr_t instr, /**< instruction */
false),
ret_value);
lit_cpointer_t prop_name_lit_cp = serializer_get_literal_cp_by_uid (prop_name_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t prop_name_lit_cp = bc_get_literal_cp_by_uid (prop_name_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
JERRY_ASSERT (prop_name_lit_cp.packed_value != MEM_CP_NULL);
ecma_string_t *prop_name_string_p = ecma_new_ecma_string_from_lit_cp (prop_name_lit_cp);
@ -1453,9 +1468,9 @@ evaluate_arg_for_typeof (vm_frame_ctx_t *frame_ctx_p, /**< interpreter context *
}
else
{
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (var_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (var_idx,
frame_ctx_p->bytecode_header_p,
frame_ctx_p->pos);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
ecma_string_t *var_name_string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
@ -1571,7 +1586,7 @@ opfunc_delete_var (vm_instr_t instr, /**< instruction */
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
lit_cpointer_t lit_cp = serializer_get_literal_cp_by_uid (name_lit_idx, frame_ctx_p->bytecode_header_p, lit_oc);
lit_cpointer_t lit_cp = bc_get_literal_cp_by_uid (name_lit_idx, frame_ctx_p->bytecode_header_p, lit_oc);
JERRY_ASSERT (lit_cp.packed_value != MEM_CP_NULL);
ecma_string_t *name_string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
@ -1718,7 +1733,6 @@ opfunc_meta (vm_instr_t instr, /**< instruction */
return ecma_make_meta_completion_value ();
}
case OPCODE_META_TYPE_SCOPE_CODE_FLAGS:
case OPCODE_META_TYPE_UNDEFINED:
case OPCODE_META_TYPE_CALL_SITE_INFO:
case OPCODE_META_TYPE_FUNCTION_END:
@ -1750,14 +1764,15 @@ vm_calc_instr_counter_from_idx_idx (const vm_idx_t oc_idx_1, /**< first idx */
} /* vm_calc_instr_counter_from_idx_idx */
/**
* Read instruction counter from current instruction,
* Read instruction counter from specified instruction,
* that should be 'meta' instruction of specified type.
*/
vm_instr_counter_t
vm_read_instr_counter_from_meta (opcode_meta_type expected_type, /**< expected type of meta instruction */
vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */
const bytecode_data_header_t *bytecode_header_p, /**< byte-code header */
vm_instr_counter_t instr_pos) /**< position of the instruction */
{
vm_instr_t meta_opcode = vm_get_instr (frame_ctx_p->bytecode_header_p->instrs_p, frame_ctx_p->pos);
vm_instr_t meta_opcode = vm_get_instr (bytecode_header_p->instrs_p, instr_pos);
JERRY_ASSERT (meta_opcode.data.meta.type == expected_type);
const vm_idx_t data_1 = meta_opcode.data.meta.data_1;

View File

@ -137,8 +137,6 @@ typedef enum
OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER, /**< literal index containing name of variable with exception object */
OPCODE_META_TYPE_FINALLY, /**< mark of beginning of finally block containing pointer to end of finally block */
OPCODE_META_TYPE_END_TRY_CATCH_FINALLY, /**< mark of end of try-catch, try-finally, try-catch-finally blocks */
OPCODE_META_TYPE_SCOPE_CODE_FLAGS, /**< set of flags indicating various properties of the scope's code
* (See also: opcode_scope_code_flags_t) */
OPCODE_META_TYPE_END_FOR_IN /**< end of for-in statement */
} opcode_meta_type;
@ -155,23 +153,6 @@ typedef enum : vm_idx_t
*/
} opcode_call_flags_t;
/**
* Flags indicating various properties of a scope's code
*/
typedef enum : vm_idx_t
{
OPCODE_SCOPE_CODE_FLAGS__EMPTY = (0u), /**< initializer for empty flag set */
OPCODE_SCOPE_CODE_FLAGS_STRICT = (1u << 0), /**< code is strict mode code */
OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER = (1u << 1), /**< code doesn't reference
* 'arguments' identifier */
OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER = (1u << 2), /**< code doesn't reference
* 'eval' identifier */
OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS = (1u << 3), /**< function's arguments are moved to registers,
* so should be initialized in vm registers,
* and not in lexical environment */
OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV = (1u << 4) /**< no lex. env. is necessary for the function */
} opcode_scope_code_flags_t;
/**
* Types of byte-code instruction arguments, used for instruction description
*
@ -246,7 +227,9 @@ typedef struct
} vm_run_scope_t;
vm_instr_counter_t vm_calc_instr_counter_from_idx_idx (const vm_idx_t, const vm_idx_t);
vm_instr_counter_t vm_read_instr_counter_from_meta (opcode_meta_type, vm_frame_ctx_t *);
vm_instr_counter_t vm_read_instr_counter_from_meta (opcode_meta_type,
const bytecode_data_header_t *,
vm_instr_counter_t);
typedef struct vm_instr_t
{

View File

@ -21,7 +21,6 @@
#include "lexer.h"
#include "ecma-helpers.h"
#include "ecma-globals.h"
#include "serializer.h"
#include "lit-literal.h"
static const char* opcode_names[] =
@ -52,6 +51,8 @@ static uint8_t opcode_sizes[] =
#include "vm-opcodes.inc.h"
};
const bytecode_data_header_t *bc_to_print_header_p = NULL;
static char buff[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
static void
@ -110,7 +111,9 @@ var_to_str (vm_instr_t instr, lit_cpointer_t lit_ids[], vm_instr_counter_t oc, u
}
else
{
return lit_cp_to_str (serializer_get_literal_cp_by_uid (instr.data.raw_args[current_arg - 1], NULL, oc));
return lit_cp_to_str (bc_get_literal_cp_by_uid (instr.data.raw_args[current_arg - 1],
bc_to_print_header_p,
oc));
}
}
@ -186,6 +189,8 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
op_meta opm,
bool rewrite)
{
bc_to_print_header_p = bytecode_data_p;
dump_asm (oc, opm.op);
printf (" // ");
@ -301,6 +306,11 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
}
break;
}
case VM_OP_FUNC_EXPR_REF:
{
printf ("%s = function ();", VAR (1));
break;
}
case VM_OP_FUNC_EXPR_N:
{
if (opm.op.data.func_expr_n.arg_list == 0)
@ -378,7 +388,7 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
while ((int16_t) start >= 0 && !found)
{
start--;
switch (serializer_get_instr (bytecode_data_p, start).op_idx)
switch (bc_get_instr (bytecode_data_p, start).op_idx)
{
case VM_OP_CALL_N:
case VM_OP_CONSTRUCT_N:
@ -392,7 +402,7 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
}
}
}
vm_instr_t start_op = serializer_get_instr (bytecode_data_p, start);
vm_instr_t start_op = bc_get_instr (bytecode_data_p, start);
switch (start_op.op_idx)
{
case VM_OP_CALL_N:
@ -439,7 +449,7 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
}
for (vm_instr_counter_t counter = start; counter <= oc; counter++)
{
vm_instr_t meta_op = serializer_get_instr (bytecode_data_p, counter);
vm_instr_t meta_op = bc_get_instr (bytecode_data_p, counter);
switch (meta_op.op_idx)
{
@ -550,48 +560,6 @@ pp_op_meta (const bytecode_data_header_t *bytecode_data_p,
printf ("end try");
break;
}
case OPCODE_META_TYPE_SCOPE_CODE_FLAGS:
{
if (opm.op.data.meta.data_1 != VM_IDX_REWRITE_GENERAL_CASE
&& opm.op.data.meta.data_1 != VM_IDX_EMPTY)
{
vm_idx_t scope_flags = opm.op.data.meta.data_1;
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT)
{
printf ("[use strict] ");
scope_flags &= (vm_idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_STRICT);
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER)
{
printf ("[no 'arguments'] ");
scope_flags &= (vm_idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER);
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER)
{
printf ("[no 'eval'] ");
scope_flags &= (vm_idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER);
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS)
{
printf ("[arguments are placed on registers] ");
scope_flags &= (vm_idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_ARGUMENTS_ON_REGISTERS);
}
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV)
{
printf ("[no lexical environment should be created for the scope] ");
scope_flags &= (vm_idx_t) ~(OPCODE_SCOPE_CODE_FLAGS_NO_LEX_ENV);
}
JERRY_ASSERT (scope_flags == 0);
}
else
{
printf ("[to be rewritten]");
}
break;
}
default:
{
JERRY_UNREACHABLE ();

View File

@ -53,6 +53,11 @@ VM_OP_3 (func_expr_n, FUNC_EXPR_N,
VM_OP_ARG_TYPE_EMPTY,
arg_list, VM_OP_ARG_TYPE_INTEGER_CONST)
VM_OP_3 (func_expr_ref, FUNC_EXPR_REF,
lhs, VM_OP_ARG_TYPE_VARIABLE,
idx1, VM_OP_ARG_TYPE_INTEGER_CONST,
idx2, VM_OP_ARG_TYPE_INTEGER_CONST)
VM_OP_1 (retval, RETVAL,
ret_value, VM_OP_ARG_TYPE_VARIABLE)

View File

@ -26,6 +26,7 @@
#include "vm.h"
#include "vm-stack.h"
#include "opcodes.h"
#include "opcodes-ecma-support.h"
/**
* Top (current) interpreter context
@ -393,16 +394,9 @@ vm_run_global (void)
interp_mem_stats_print_legend ();
#endif /* MEM_STATS */
bool is_strict = false;
bool is_strict = __program->is_strict;
vm_instr_counter_t start_pos = 0;
opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (__program, start_pos++);
if (scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT)
{
is_strict = true;
}
ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL);
ecma_object_t *lex_env_p = ecma_get_global_environment ();
@ -449,8 +443,8 @@ vm_run_eval (const bytecode_data_header_t *bytecode_data_p, /**< byte-code data
bool is_direct) /**< is eval called in direct mode? */
{
vm_instr_counter_t first_instr_index = 0u;
opcode_scope_code_flags_t scope_flags = vm_get_scope_flags (bytecode_data_p, first_instr_index++);
bool is_strict = ((scope_flags & OPCODE_SCOPE_CODE_FLAGS_STRICT) != 0);
bool is_strict = bytecode_data_p->is_strict;
ecma_value_t this_binding;
ecma_object_t *lex_env_p;
@ -605,57 +599,116 @@ vm_run_from_pos (const bytecode_data_header_t *header_p, /**< byte-code data hea
* - NULL - otherwise.
*/
{
ecma_completion_value_t completion;
ecma_completion_value_t completion = ecma_make_empty_completion_value ();
const vm_instr_t *instrs_p = header_p->instrs_p;
const vm_instr_t *curr = &instrs_p[start_pos];
JERRY_ASSERT (curr->op_idx == VM_OP_REG_VAR_DECL);
const uint32_t tmp_regs_num = curr->data.reg_var_decl.tmp_regs_num;
const uint32_t local_var_regs_num = curr->data.reg_var_decl.local_var_regs_num;
const uint32_t arg_regs_num = curr->data.reg_var_decl.arg_regs_num;
mem_cpointer_t *declarations_p = MEM_CP_GET_POINTER (mem_cpointer_t, header_p->declarations_cp);
for (uint16_t func_scope_index = 0;
func_scope_index < header_p->func_scopes_count && ecma_is_completion_value_empty (completion);
func_scope_index++)
{
bytecode_data_header_t *func_bc_header_p = MEM_CP_GET_NON_NULL_POINTER (bytecode_data_header_t,
declarations_p[func_scope_index]);
uint32_t regs_num = VM_SPECIAL_REGS_NUMBER + tmp_regs_num + local_var_regs_num + arg_regs_num;
if (func_bc_header_p->instrs_p[0].op_idx == VM_OP_FUNC_DECL_N)
{
completion = vm_function_declaration (func_bc_header_p,
is_strict,
is_eval_code,
lex_env_p);
MEM_DEFINE_LOCAL_ARRAY (regs, regs_num, ecma_value_t);
}
}
vm_frame_ctx_t frame_ctx;
frame_ctx.bytecode_header_p = header_p;
frame_ctx.pos = (vm_instr_counter_t) (start_pos + 1);
frame_ctx.lex_env_p = lex_env_p;
frame_ctx.is_strict = is_strict;
frame_ctx.is_eval_code = is_eval_code;
frame_ctx.is_call_in_direct_eval_form = false;
frame_ctx.tmp_num_p = ecma_alloc_number ();
lit_cpointer_t *lit_ids_p = (lit_cpointer_t *) (declarations_p + header_p->func_scopes_count);
for (uint16_t var_decl_index = 0;
var_decl_index < header_p->var_decls_count && ecma_is_completion_value_empty (completion);
var_decl_index++)
{
lit_cpointer_t lit_cp = lit_ids_p[var_decl_index];
vm_stack_add_frame (&frame_ctx.stack_frame, regs, regs_num, local_var_regs_num, arg_regs_num, arg_collection_p);
vm_stack_frame_set_reg_value (&frame_ctx.stack_frame,
VM_REG_SPECIAL_THIS_BINDING,
ecma_copy_value (this_binding_value, false));
if (lit_cp.packed_value != NOT_A_LITERAL.packed_value)
{
ecma_string_t *var_name_string_p = ecma_new_ecma_string_from_lit_cp (lit_cp);
vm_frame_ctx_t *prev_context_p = vm_top_context_p;
vm_top_context_p = &frame_ctx;
if (!ecma_op_has_binding (lex_env_p, var_name_string_p))
{
const bool is_configurable_bindings = is_eval_code;
completion = ecma_op_create_mutable_binding (lex_env_p,
var_name_string_p,
is_configurable_bindings);
JERRY_ASSERT (ecma_is_completion_value_empty (completion));
/* Skipping SetMutableBinding as we have already checked that there were not
* any binding with specified name in current lexical environment
* and CreateMutableBinding sets the created binding's value to undefined */
JERRY_ASSERT (ecma_is_completion_value_normal_simple_value (ecma_op_get_binding_value (lex_env_p,
var_name_string_p,
true),
ECMA_SIMPLE_VALUE_UNDEFINED));
}
ecma_deref_ecma_string (var_name_string_p);
}
}
if (!ecma_is_completion_value_empty (completion))
{
JERRY_ASSERT (ecma_is_completion_value_throw (completion));
}
else
{
const uint32_t tmp_regs_num = curr->data.reg_var_decl.tmp_regs_num;
const uint32_t local_var_regs_num = curr->data.reg_var_decl.local_var_regs_num;
const uint32_t arg_regs_num = curr->data.reg_var_decl.arg_regs_num;
uint32_t regs_num = VM_SPECIAL_REGS_NUMBER + tmp_regs_num + local_var_regs_num + arg_regs_num;
MEM_DEFINE_LOCAL_ARRAY (regs, regs_num, ecma_value_t);
vm_frame_ctx_t frame_ctx;
frame_ctx.bytecode_header_p = header_p;
frame_ctx.pos = (vm_instr_counter_t) (start_pos + 1);
frame_ctx.lex_env_p = lex_env_p;
frame_ctx.is_strict = is_strict;
frame_ctx.is_eval_code = is_eval_code;
frame_ctx.is_call_in_direct_eval_form = false;
frame_ctx.tmp_num_p = ecma_alloc_number ();
vm_stack_add_frame (&frame_ctx.stack_frame, regs, regs_num, local_var_regs_num, arg_regs_num, arg_collection_p);
vm_stack_frame_set_reg_value (&frame_ctx.stack_frame,
VM_REG_SPECIAL_THIS_BINDING,
ecma_copy_value (this_binding_value, false));
vm_frame_ctx_t *prev_context_p = vm_top_context_p;
vm_top_context_p = &frame_ctx;
#ifdef MEM_STATS
interp_mem_stats_context_enter (&frame_ctx, start_pos);
interp_mem_stats_context_enter (&frame_ctx, start_pos);
#endif /* MEM_STATS */
completion = vm_loop (&frame_ctx, NULL);
completion = vm_loop (&frame_ctx, NULL);
JERRY_ASSERT (ecma_is_completion_value_throw (completion)
|| ecma_is_completion_value_return (completion));
JERRY_ASSERT (ecma_is_completion_value_throw (completion)
|| ecma_is_completion_value_return (completion));
vm_top_context_p = prev_context_p;
vm_top_context_p = prev_context_p;
vm_stack_free_frame (&frame_ctx.stack_frame);
vm_stack_free_frame (&frame_ctx.stack_frame);
ecma_dealloc_number (frame_ctx.tmp_num_p);
ecma_dealloc_number (frame_ctx.tmp_num_p);
#ifdef MEM_STATS
interp_mem_stats_context_exit (&frame_ctx, start_pos);
interp_mem_stats_context_exit (&frame_ctx, start_pos);
#endif /* MEM_STATS */
MEM_FINALIZE_LOCAL_ARRAY (regs);
MEM_FINALIZE_LOCAL_ARRAY (regs);
}
return completion;
} /* vm_run_from_pos */
@ -670,21 +723,6 @@ vm_get_instr (const vm_instr_t *instrs_p, /**< byte-code array */
return instrs_p[ counter ];
} /* vm_get_instr */
/**
* Get scope code flags from instruction at specified position
*
* @return mask of scope code flags
*/
opcode_scope_code_flags_t
vm_get_scope_flags (const bytecode_data_header_t *bytecode_header_p, /**< byte-code data */
vm_instr_counter_t counter) /**< instruction counter */
{
vm_instr_t flags_instr = vm_get_instr (bytecode_header_p->instrs_p, counter);
JERRY_ASSERT (flags_instr.op_idx == VM_OP_META
&& flags_instr.data.meta.type == OPCODE_META_TYPE_SCOPE_CODE_FLAGS);
return (opcode_scope_code_flags_t) flags_instr.data.meta.data_1;
} /* vm_get_scope_flags */
/**
* Get arguments number, encoded in specified reg_var_decl instruction
*

View File

@ -30,7 +30,6 @@ extern ecma_completion_value_t vm_run_from_pos (const bytecode_data_header_t *,
ecma_value_t, ecma_object_t *, bool, bool, ecma_collection_header_t *);
extern vm_instr_t vm_get_instr (const vm_instr_t *, vm_instr_counter_t);
extern opcode_scope_code_flags_t vm_get_scope_flags (const bytecode_data_header_t *, vm_instr_counter_t);
extern uint8_t vm_get_scope_args_num (const bytecode_data_header_t *, vm_instr_counter_t);
extern bool vm_is_strict_mode (void);

View File

@ -13,9 +13,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
try {
eval('v_0 = {0: delete 3. instanceof foo() ^ {} }');
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
function check_reference_error (s) {
try {
eval (s);
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
}
}
check_reference_error ('v_0 = {0: delete 3. instanceof foo() ^ {} }');

View File

@ -0,0 +1,22 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// 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.
function a()
{
for (i = 1; i < 5; i++)
if (i) {
5;
}
}

View File

@ -0,0 +1,19 @@
// 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.
var a;
(a) = 1;
assert (a === 1);

View File

@ -0,0 +1,68 @@
// 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.
function check_syntax_error (s) {
try {
eval (s);
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
}
/* Test case #1 */
check_syntax_error (
" new function f(f) { \
return {className: 'xxx'}; \
}; \
x = 1; \
function g(active) { \
for (i = 1; i <= 1000; i++) { if (i == active) { \
x = i; if (f(\"\" + i) != null) { } \
} else { \
if (f(\"\" + i) != null) } \
} \
} \
g(0) \
");
/* Test case #2 */
check_syntax_error (
" new function a(a) {;for (f in [1,2,3]) print(f); \
}; 1; \
function g(active) { \
for (i = 1; i <= 1000; i++) { if (i == active) { \
xI \
if (f != null) { } \
} else { \
if (f(\"\" + i) != null) } \
} \
} \
g(0) \
");
/* Test case #3 */
check_syntax_error (
" new function f(f) {;for (f in [1,2,3]) pRint(f); \
}; 1; \
function g(active) { \
for (i = 1; i <= 1000; i++) { if (i == active) { \
x \
if (f != null) { } \
} else { \
if (f(\"\" + i) != null) } \
} \
} \
g(0) \
");

View File

@ -48,6 +48,14 @@ switch (a) {
assert (0);
}
switch (a) {
case 3:
assert (0);
default:
assert (0);
case 1:
}
executed_case = '';
switch (a) {
default:

View File

@ -685,55 +685,59 @@ main (void)
jerry_cleanup ();
// Dump / execute snapshot
static uint8_t global_mode_snapshot_buffer[1024];
static uint8_t eval_mode_snapshot_buffer[1024];
// FIXME: support save/load snapshot for optimized parser
if (false)
{
static uint8_t global_mode_snapshot_buffer[1024];
static uint8_t eval_mode_snapshot_buffer[1024];
const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();";
const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();";
jerry_init (JERRY_FLAG_SHOW_OPCODES);
size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p,
jerry_init (JERRY_FLAG_SHOW_OPCODES);
size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p,
strlen (code_to_snapshot_p),
true,
global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer));
JERRY_ASSERT (global_mode_snapshot_size != 0);
jerry_cleanup ();
jerry_init (JERRY_FLAG_SHOW_OPCODES);
size_t eval_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p,
strlen (code_to_snapshot_p),
true,
global_mode_snapshot_buffer,
sizeof (global_mode_snapshot_buffer));
JERRY_ASSERT (global_mode_snapshot_size != 0);
jerry_cleanup ();
false,
eval_mode_snapshot_buffer,
sizeof (eval_mode_snapshot_buffer));
JERRY_ASSERT (eval_mode_snapshot_size != 0);
jerry_cleanup ();
jerry_init (JERRY_FLAG_SHOW_OPCODES);
size_t eval_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p,
strlen (code_to_snapshot_p),
false,
eval_mode_snapshot_buffer,
sizeof (eval_mode_snapshot_buffer));
JERRY_ASSERT (eval_mode_snapshot_size != 0);
jerry_cleanup ();
jerry_init (JERRY_FLAG_SHOW_OPCODES);
jerry_init (JERRY_FLAG_SHOW_OPCODES);
is_ok = (jerry_exec_snapshot (global_mode_snapshot_buffer,
global_mode_snapshot_size,
false,
&res) == JERRY_COMPLETION_CODE_OK);
is_ok = (jerry_exec_snapshot (global_mode_snapshot_buffer,
global_mode_snapshot_size,
false,
&res) == JERRY_COMPLETION_CODE_OK);
JERRY_ASSERT (is_ok
&& res.type == JERRY_API_DATA_TYPE_UNDEFINED);
jerry_api_release_value (&res);
JERRY_ASSERT (is_ok
&& res.type == JERRY_API_DATA_TYPE_UNDEFINED);
jerry_api_release_value (&res);
is_ok = (jerry_exec_snapshot (eval_mode_snapshot_buffer,
eval_mode_snapshot_size,
false,
&res) == JERRY_COMPLETION_CODE_OK);
is_ok = (jerry_exec_snapshot (eval_mode_snapshot_buffer,
eval_mode_snapshot_size,
false,
&res) == JERRY_COMPLETION_CODE_OK);
JERRY_ASSERT (is_ok
&& res.type == JERRY_API_DATA_TYPE_STRING);
sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0);
JERRY_ASSERT (sz == -20);
sz = jerry_api_string_to_char_buffer (res.v_string, (jerry_api_char_t *) buffer, -sz);
JERRY_ASSERT (sz == 20);
jerry_api_release_value (&res);
JERRY_ASSERT (!strncmp (buffer, "string from snapshot", (size_t) sz));
JERRY_ASSERT (is_ok
&& res.type == JERRY_API_DATA_TYPE_STRING);
sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0);
JERRY_ASSERT (sz == -20);
sz = jerry_api_string_to_char_buffer (res.v_string, (jerry_api_char_t *) buffer, -sz);
JERRY_ASSERT (sz == 20);
jerry_api_release_value (&res);
JERRY_ASSERT (!strncmp (buffer, "string from snapshot", (size_t) sz));
jerry_cleanup ();
jerry_cleanup ();
}
return 0;
}

View File

@ -13,11 +13,10 @@
* limitations under the License.
*/
#include "bytecode-data.h"
#include "mem-allocator.h"
#include "opcodes.h"
#include "parser.h"
#include "serializer.h"
#include "test-common.h"
static bool
@ -109,7 +108,7 @@ main (int __attr_unused___ argc,
// #1
char program1[] = "a=1;var a;";
serializer_init ();
lit_init ();
parser_set_show_instrs (true);
parse_status = parser_parse_script ((jerry_api_char_t *) program1, strlen (program1), &bytecode_data_p);
@ -117,31 +116,27 @@ main (int __attr_unused___ argc,
vm_instr_t instrs[] =
{
getop_meta (OPCODE_META_TYPE_SCOPE_CODE_FLAGS, // [ ]
OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER
| OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER,
VM_IDX_EMPTY),
getop_reg_var_decl (1u, 0u, 0u),
getop_var_decl (0), // var a;
getop_assignment (VM_REG_GENERAL_FIRST, 1, 1), // $tmp0 = 1;
getop_assignment (0, 6, VM_REG_GENERAL_FIRST), // a = $tmp0;
getop_assignment (0, 1, 1), // a = 1 (SMALLINT);
getop_ret () // return;
};
JERRY_ASSERT (instrs_equal (bytecode_data_p->instrs_p, instrs, 5));
JERRY_ASSERT (instrs_equal (bytecode_data_p->instrs_p, instrs, 3));
serializer_free ();
lit_finalize ();
bc_finalize ();
// #2
char program2[] = "var var;";
serializer_init ();
lit_init ();
parser_set_show_instrs (true);
parse_status = parser_parse_script ((jerry_api_char_t *) program2, strlen (program2), &bytecode_data_p);
JERRY_ASSERT (parse_status == JSP_STATUS_SYNTAX_ERROR && bytecode_data_p == NULL);
serializer_free ();
lit_finalize ();
bc_finalize ();
mem_finalize (false);