Postparser landing patch: add tree of scopes

This commit is contained in:
Ilmir Usmanov
2014-10-12 17:53:02 +04:00
parent cb7e54fe22
commit 63662dfb35
12 changed files with 305 additions and 48 deletions
+3 -3
View File
@@ -1300,9 +1300,9 @@ lexer_init (const char *source, size_t source_size, bool show_opcodes)
buffer_size = source_size;
lexer_set_source (source);
STACK_INIT (lp_string, strings);
STACK_INIT (ecma_number_t, numbers);
STACK_INIT (idx_t, num_ids);
STACK_INIT (strings);
STACK_INIT (numbers);
STACK_INIT (num_ids);
}
void
+31 -10
View File
@@ -25,6 +25,7 @@
#include "deserializer.h"
#include "opcodes-native-call.h"
#include "parse-error.h"
#include "scopes-tree.h"
#define INVALID_VALUE 255
#define INTRINSICS_COUNT 1
@@ -76,6 +77,14 @@ enum
};
STATIC_STACK (nestings, uint8_t, uint8_t)
typedef scopes_tree* scopes_tree_p;
enum
{
scopes_global_size
};
STATIC_STACK (scopes, uint8_t, scopes_tree_p)
enum
{
temp_name,
@@ -3592,6 +3601,10 @@ void
parser_parse_program (void)
{
STACK_DECLARE_USAGE (IDX)
STACK_DECLARE_USAGE (scopes);
STACK_PUSH (scopes, scopes_tree_init (NULL));
serializer_set_scope (STACK_TOP (scopes));
skip_newlines ();
parse_source_element_list (true);
@@ -3601,8 +3614,14 @@ parser_parse_program (void)
DUMP_OPCODE_1 (exitval, 0);
serializer_adjust_strings ();
serializer_merge_scopes_into_bytecode ();
scopes_tree_free (STACK_TOP (scopes));
serializer_set_scope (NULL);
STACK_DROP (scopes, 1);
STACK_CHECK_USAGE (IDX);
STACK_CHECK_USAGE (scopes);
}
void
@@ -3620,16 +3639,17 @@ parser_init (const char *source, size_t source_size, bool show_opcodes)
serializer_dump_strings_and_nums (identifiers, lexer_get_strings_count (),
lexer_get_nums (), lexer_get_nums_count ());
STACK_INIT (uint8_t, U8);
STACK_INIT (uint8_t, IDX);
STACK_INIT (uint8_t, nestings);
STACK_INIT (uint8_t, temp_names);
STACK_INIT (token, toks);
STACK_INIT (opcode_t, ops);
STACK_INIT (uint16_t, U16);
STACK_INIT (opcode_counter_t, rewritable_continue);
STACK_INIT (opcode_counter_t, rewritable_break);
STACK_INIT (locus, locs);
STACK_INIT (U8);
STACK_INIT (IDX);
STACK_INIT (nestings);
STACK_INIT (temp_names);
STACK_INIT (toks);
STACK_INIT (ops);
STACK_INIT (U16);
STACK_INIT (rewritable_continue);
STACK_INIT (rewritable_break);
STACK_INIT (locs);
STACK_INIT (scopes);
HASH_INIT (intrinsics, 1);
@@ -3689,6 +3709,7 @@ parser_free (void)
STACK_FREE (rewritable_continue);
STACK_FREE (rewritable_break);
STACK_FREE (locs);
STACK_FREE (scopes);
HASH_FREE (intrinsics);
+190
View File
@@ -0,0 +1,190 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "scopes-tree.h"
#include "mem-heap.h"
#include "jerry-libc.h"
#define NAME_TO_ID(op) (__op__idx_##op)
static void
assert_tree (scopes_tree *t)
{
JERRY_ASSERT (t != NULL);
JERRY_ASSERT (t->t.magic == TREE_MAGIC);
}
opcode_counter_t
scopes_tree_opcodes_num (scopes_tree *t)
{
assert_tree (t);
return t->opcodes_num;
}
void
scopes_tree_add_opcode (scopes_tree *tree, opcode_t op)
{
assert_tree (tree);
linked_list_set_element (tree->opcodes, sizeof (opcode_t), tree->opcodes_num++, &op);
}
void
scopes_tree_set_opcode (scopes_tree *tree, opcode_counter_t oc, opcode_t op)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->opcodes_num);
linked_list_set_element (tree->opcodes, sizeof (opcode_t), oc, &op);
}
void
scopes_tree_set_opcodes_num (scopes_tree *tree, opcode_counter_t oc)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->opcodes_num);
tree->opcodes_num = oc;
}
opcode_t
scopes_tree_opcode (scopes_tree *tree, opcode_counter_t oc)
{
assert_tree (tree);
JERRY_ASSERT (oc < tree->opcodes_num);
return *(opcode_t *) linked_list_element (tree->opcodes, sizeof (opcode_t), oc);
}
static opcode_counter_t
count_opcodes_in_tree (scopes_tree *t)
{
assert_tree (t);
opcode_counter_t res = t->opcodes_num;
for (uint8_t i = 0; i < t->t.children_num; i++)
{
res = (opcode_counter_t) (res
+ count_opcodes_in_tree ((scopes_tree *) linked_list_element (t->t.children,
sizeof (scopes_tree *),
i)));
}
return res;
}
static opcode_counter_t
merge_subscopes (scopes_tree *tree, opcode_t *data)
{
assert_tree (tree);
JERRY_ASSERT (data);
bool header = true;
opcode_counter_t opc_index, data_index;
for (opc_index = 0, data_index = 0; opc_index < tree->opcodes_num; opc_index++, data_index++)
{
opcode_t *op = (opcode_t *) linked_list_element (tree->opcodes, sizeof (opcode_t), opc_index);
JERRY_ASSERT (op);
switch (opc_index)
{
case 0:
{
JERRY_ASSERT (op->op_idx == NAME_TO_ID (reg_var_decl));
data[data_index] = *op;
break;
}
case 1:
{
JERRY_ASSERT (op->op_idx == NAME_TO_ID (nop) || op->op_idx == NAME_TO_ID (meta));
data[data_index] = *op;
break;
}
default:
{
if (op->op_idx == NAME_TO_ID (var_decl))
{
data[data_index] = *op;
}
else
{
header = false;
}
}
}
if (!header)
{
break;
}
}
for (uint8_t child_id = 0; child_id < tree->t.children_num; child_id++)
{
data_index = (opcode_counter_t) (data_index
+ merge_subscopes ((scopes_tree *) linked_list_element (tree->t.children,
sizeof (scopes_tree *),
child_id),
data + data_index));
}
for (; opc_index < tree->opcodes_num; opc_index++, data_index++)
{
opcode_t *op = (opcode_t *) linked_list_element (tree->opcodes, sizeof (opcode_t), opc_index);
data[data_index] = *op;
}
return data_index;
}
opcode_t *
scopes_tree_raw_data (scopes_tree *tree, opcode_counter_t *num)
{
assert_tree (tree);
opcode_counter_t res = count_opcodes_in_tree (tree);
size_t size = ((size_t) (res + 1) * sizeof (opcode_t)); // +1 for valgrind
opcode_t *opcodes = (opcode_t *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_LONG_TERM);
__memset (opcodes, 0, size);
opcode_counter_t merged = merge_subscopes (tree, opcodes);
JERRY_ASSERT (merged == res);
*num = res;
return opcodes;
}
scopes_tree *
scopes_tree_init (scopes_tree *parent)
{
scopes_tree *tree = (scopes_tree *) mem_heap_alloc_block (sizeof (scopes_tree), MEM_HEAP_ALLOC_SHORT_TERM);
__memset (tree, 0, sizeof (scopes_tree));
tree->t.magic = TREE_MAGIC;
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, sizeof (scopes_tree *), parent->t.children_num++, tree);
}
tree->opcodes_num = 0;
tree->opcodes = linked_list_init (sizeof (opcode_t));
return tree;
}
void
scopes_tree_free (scopes_tree *tree)
{
assert_tree (tree);
for (uint8_t i = 0; i < tree->t.children_num; ++i)
{
scopes_tree_free ((scopes_tree *) linked_list_element (tree->t.children, sizeof (scopes_tree *), i));
}
if (tree->t.children_num != 0)
{
linked_list_free (tree->t.children);
}
linked_list_free (tree->opcodes);
mem_heap_free_block ((uint8_t *) tree);
}
+41
View File
@@ -0,0 +1,41 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SCOPES_TREE_H
#define SCOPES_TREE_H
#include "tree.h"
#include "linked-list.h"
#include "opcodes.h"
typedef struct scopes_tree
{
tree_header t;
opcode_counter_t opcodes_num;
linked_list opcodes;
}
__packed
scopes_tree;
scopes_tree *scopes_tree_init (scopes_tree *);
void scopes_tree_free (scopes_tree *);
opcode_counter_t scopes_tree_opcodes_num (scopes_tree *);
void scopes_tree_add_opcode (scopes_tree *, opcode_t);
void scopes_tree_set_opcode (scopes_tree *, opcode_counter_t, opcode_t);
void scopes_tree_set_opcodes_num (scopes_tree *, opcode_counter_t);
opcode_t scopes_tree_opcode (scopes_tree *, opcode_counter_t);
opcode_t *scopes_tree_raw_data (scopes_tree *, opcode_counter_t *);
#endif /* SCOPES_TREE_H */