Example #1
0
void
serializer_merge_scopes_into_bytecode (void)
{
  JERRY_ASSERT (bytecode_data.lit_id_hash == null_hash);
  bytecode_data.opcodes_count = scopes_tree_count_opcodes (current_scope);
  bytecode_data.lit_id_hash = lit_id_hash_table_init (scopes_tree_count_literals_in_blocks (current_scope),
                                                      bytecode_data.opcodes_count);
  bytecode_data.opcodes = scopes_tree_raw_data (current_scope, bytecode_data.lit_id_hash);
}
Example #2
0
/**
 * 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 < tree->var_decls_cout; 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 */
Example #3
0
/**
 * 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 */
Example #4
0
const vm_instr_t *
serializer_merge_scopes_into_bytecode (void)
{
  bytecode_data.instrs_count = scopes_tree_count_instructions (current_scope);

  const size_t buckets_count = scopes_tree_count_literals_in_blocks (current_scope);
  const size_t blocks_count = (size_t) bytecode_data.instrs_count / BLOCK_SIZE + 1;
  const vm_instr_counter_t instrs_count = scopes_tree_count_instructions (current_scope);

  const size_t bytecode_array_size = JERRY_ALIGNUP (sizeof (insts_data_header_t) + instrs_count * sizeof (vm_instr_t),
                                                    MEM_ALIGNMENT);
  const size_t lit_id_hash_table_size = JERRY_ALIGNUP (lit_id_hash_table_get_size_for_table (buckets_count,
                                                                                             blocks_count),
                                                       MEM_ALIGNMENT);

  uint8_t *buffer_p = (uint8_t*) mem_heap_alloc_block (bytecode_array_size + lit_id_hash_table_size,
                                                       MEM_HEAP_ALLOC_LONG_TERM);

  lit_id_hash_table *lit_id_hash = lit_id_hash_table_init (buffer_p + bytecode_array_size,
                                                           lit_id_hash_table_size,
                                                           buckets_count, blocks_count);

  const vm_instr_t *instrs_p = scopes_tree_raw_data (current_scope, buffer_p, bytecode_array_size, lit_id_hash);

  insts_data_header_t *header_p = (insts_data_header_t*) buffer_p;
  MEM_CP_SET_POINTER (header_p->next_instrs_cp, bytecode_data.instrs_p);
  header_p->instructions_number = instrs_count;
  bytecode_data.instrs_p = instrs_p;

  if (print_instrs)
  {
    lit_dump_literals ();
    serializer_print_instrs (instrs_p, bytecode_data.instrs_count);
  }

  return instrs_p;
}