Move serializer and deserializer to liboptimizer

This commit is contained in:
Ilmir Usmanov
2014-08-06 20:32:16 +04:00
parent ba6713e295
commit 5690be49dd
10 changed files with 2 additions and 254 deletions
+37
View File
@@ -0,0 +1,37 @@
/* 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 BYTECODE_LINUX_H
#define BYTECODE_LINUX_H
#include "opcodes.h"
#define MAX_OPCODES 255
/* bytecode_data contains identifiers, string and num literals.
Memory map if the following.
bytecode_data {
U8 strs_count;
U8 string_offsets[str_count];
U8* strings[str_count];
U8 nums_count;
U32 nums[nums_count];
} */
uint8_t *bytecode_data;
OPCODE bytecode_opcodes[MAX_OPCODES];
#endif // BYTECODE_LINUX_H
+94
View File
@@ -0,0 +1,94 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "deserializer.h"
#include "bytecode-data.h"
int *num_data = NULL;
uint8_t num_size = 0;
const ecma_char_t *
deserialize_string_by_id (uint8_t id)
{
uint8_t size, *data;
uint16_t offset;
if (bytecode_data == NULL)
return NULL;
size = *bytecode_data;
if (id >= size)
return NULL;
data = bytecode_data;
data += id * 2 + 1;
offset = *((uint16_t *) data);
return ((const ecma_char_t *) bytecode_data + offset);
}
int
deserialize_num_by_id (uint8_t id)
{
uint16_t str_size, str_offset;
str_size = *bytecode_data;
if (id < str_size)
{
return 0;
}
id = (uint8_t) (id - str_size);
if (num_data == NULL)
{
// Go to last string's offset
uint8_t *data = (uint8_t *) (bytecode_data + str_size * 2 - 1);
str_offset = *((uint16_t *) data);
data = bytecode_data + str_offset;
while (*data)
data++;
num_size = *(++data);
num_data = (int *) ++data;
}
if (id >= num_size)
return 0;
return num_data[id];
}
const void *
deserialize_bytecode (void)
{
return bytecode_opcodes;
}
uint8_t
deserialize_min_temp (void)
{
uint8_t str_size = *bytecode_data;
if (num_size == 0)
{
deserialize_num_by_id (str_size); // Init num_data and num_size
}
return (uint8_t) (str_size + num_size);
}
+27
View File
@@ -0,0 +1,27 @@
/* 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 DESERIALIZER_H
#define DESERIALIZER_H
#include "globals.h"
#include "ecma-globals.h"
const ecma_char_t *deserialize_string_by_id (uint8_t);
int deserialize_num_by_id (uint8_t);
const void *deserialize_bytecode (void);
uint8_t deserialize_min_temp (void);
#endif //DESERIALIZER_H
+447
View File
@@ -0,0 +1,447 @@
/* 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 "pretty-printer.h"
#include "jerry-libc.h"
#include "deserializer.h"
#define NAME_TO_ID(op) (__op__idx_##op)
#define FIELD(op, field) (opcode.data.op.field)
#define OPCODE_STR(op) \
#op,
#define OPCODE_SIZE(op) \
sizeof (struct __op_##op) + 1,
static char* opcode_names[] = {
OP_LIST (OPCODE_STR)
""
};
static uint8_t opcode_sizes[] = {
OP_LIST (OPCODE_SIZE)
0
};
void
pp_strings (const char *strings[], uint8_t size)
{
uint8_t i;
uint16_t offset = (uint16_t) (size * 2 + 1);
__printf ("STRINGS %d:\n", size);
for (i = 0; i < size; i++)
{
__printf ("%3d %5d %20s\n", i, offset, strings[i]);
offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
}
}
void
pp_nums (const int32_t nums[], uint8_t size, uint8_t strings_num)
{
uint8_t i;
__printf ("NUMS %d:\n", size);
for (i = 0; i < size; i++)
{
__printf ("%3d %7d\n", i + strings_num, nums[i]);
}
__printf ("\n");
}
static void
dump_variable (T_IDX id)
{
if (id >= deserialize_min_temp ())
__printf ("tmp%d", id);
else if (deserialize_string_by_id (id))
__printf ("%s", deserialize_string_by_id (id));
else
__printf ("%d", deserialize_num_by_id (id));
}
#define CASE_CONDITIONAL_JUMP(op, string1, field1, string2, field2) \
case NAME_TO_ID (op): \
__printf (string1); \
dump_variable (opcode.data.op.field1); \
__printf (string2); \
__printf (" %d;", opcode.data.op.field2); \
break;
#define CASE_UNCONDITIONAL_JUMP(op, string, oc, oper, field) \
case NAME_TO_ID (op): \
__printf (string); \
__printf (" %d;", oc oper opcode.data.op.field); \
break;
#define CASE_TRIPLE_ADDRESS(op, lhs, equals, op1, oper, op2) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " "); \
dump_variable (opcode.data.op.op1); \
__printf (" " oper " "); \
dump_variable (opcode.data.op.op2); \
__printf (";"); \
break;
#define CASE_DOUBLE_ADDRESS(op, lhs, equals, oper, op2) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " "); \
__printf (oper " "); \
dump_variable (opcode.data.op.op2); \
__printf (";"); \
break;
#define CASE_DOUBLE_ADDRESS_POST(op, lhs, equals, op2, oper) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " "); \
dump_variable (opcode.data.op.op2); \
__printf (" " oper ";"); \
break;
#define CASE_ASSIGNMENT(op, lhs, equals, op2) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " "); \
if (opcode.data.op.type_value_right == OPCODE_ARG_TYPE_SIMPLE) { \
switch (opcode.data.op.op2) { \
case ECMA_SIMPLE_VALUE_NULL: \
__printf ("null"); \
break; \
case ECMA_SIMPLE_VALUE_FALSE: \
__printf ("false"); \
break; \
case ECMA_SIMPLE_VALUE_TRUE: \
__printf ("true"); \
break; \
default: \
JERRY_UNREACHABLE (); \
} \
__printf (": SIMPLE"); \
} else if (opcode.data.op.type_value_right == OPCODE_ARG_TYPE_STRING) { \
__printf ("'%s'", deserialize_string_by_id (opcode.data.op.op2)); \
__printf (": STRING"); \
} else if (opcode.data.op.type_value_right == OPCODE_ARG_TYPE_NUMBER) {\
__printf ("%d", deserialize_num_by_id (opcode.data.op.op2)); \
__printf (": NUMBER"); \
} else if (opcode.data.op.type_value_right == OPCODE_ARG_TYPE_SMALLINT) {\
__printf ("%d", opcode.data.op.op2); \
__printf (": NUMBER"); \
} else if (opcode.data.op.type_value_right == OPCODE_ARG_TYPE_VARIABLE) {\
dump_variable (opcode.data.op.op2); \
__printf (": TYPEOF("); \
dump_variable (opcode.data.op.op2); \
__printf (")"); \
} else { \
JERRY_UNREACHABLE (); \
} \
__printf (";"); \
break;
#define CASE_VARG_0_NAME_LHS(op, lhs, equals, new, name, start, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " new " "); \
dump_variable (opcode.data.op.name); \
__printf (start end ";"); \
break;
#define CASE_VARG_1_NAME_LHS(op, lhs, equals, new, name, start, arg, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " new " "); \
dump_variable (opcode.data.op.name); \
__printf (start); \
dump_variable (opcode.data.op.arg); \
__printf (end ";"); \
break;
#define CASE_VARG_N_NAME_LHS(op, lhs, equals, new, name, start, arg, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " new " "); \
dump_variable (opcode.data.op.name); \
__printf (start); \
dump_variable (opcode.data.op.arg); \
__printf (" ..."); \
varg_end = end; \
break;
#define CASE_VARG_0_NAME(op, new, name, start, end) \
case NAME_TO_ID (op): \
__printf (new " "); \
dump_variable (opcode.data.op.name); \
__printf (start end ";"); \
break;
#define CASE_VARG_1_NAME(op, new, name, start, arg1, end) \
case NAME_TO_ID (op): \
__printf (new " "); \
dump_variable (opcode.data.op.name); \
__printf (start); \
dump_variable (opcode.data.op.arg1); \
__printf (end ";"); \
break;
#define CASE_VARG_2_NAME(op, new, name, start, arg1, arg2, end) \
case NAME_TO_ID (op): \
__printf (new " "); \
dump_variable (opcode.data.op.name); \
__printf (start); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (end ";"); \
break;
#define CASE_VARG_N_NAME(op, new, name, start, arg1, arg2, end) \
case NAME_TO_ID (op): \
__printf (new " "); \
dump_variable (opcode.data.op.name); \
__printf (start); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (" ..."); \
varg_end = end; \
break;
#define CASE_VARG_0_LHS(op, lhs, equals, start, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " start); \
__printf (end ";"); \
break;
#define CASE_VARG_1_LHS(op, lhs, equals, start, arg1, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " start); \
dump_variable (opcode.data.op.arg1); \
__printf (end ";"); \
break;
#define CASE_VARG_2_LHS(op, lhs, equals, start, arg1, arg2, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " start); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (end ";"); \
break;
#define CASE_VARG_N_LHS(op, lhs, equals, start, arg1, arg2, end) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " " start); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (" ..."); \
varg_end = end; \
break;
#define CASE_VARG_1_END(op, arg1) \
case NAME_TO_ID (op): \
__printf ("... "); \
dump_variable (opcode.data.op.arg1); \
__printf ("%s;", varg_end); \
break;
#define CASE_VARG_2_END(op, arg1, arg2) \
case NAME_TO_ID (op): \
__printf ("... "); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf ("%s;", varg_end); \
break;
#define CASE_VARG_3_END(op, arg1, arg2, arg3) \
case NAME_TO_ID (op): \
__printf ("... "); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (", "); \
dump_variable (opcode.data.op.arg3); \
__printf ("%s;", varg_end); \
break;
#define CASE_VARG_3(op, arg1, arg2, arg3) \
case NAME_TO_ID (op): \
__printf ("... "); \
dump_variable (opcode.data.op.arg1); \
__printf (", "); \
dump_variable (opcode.data.op.arg2); \
__printf (", "); \
dump_variable (opcode.data.op.arg3); \
__printf (" ..."); \
break;
#define CASE_EXIT(op, name, field) \
case NAME_TO_ID (op): \
__printf (name " "); \
__printf ("%d;", opcode.data.op.field); \
break;
#define CASE_SINGLE_ADDRESS(op, name, field) \
case NAME_TO_ID (op): \
__printf (name " "); \
dump_variable (opcode.data.op.field); \
__printf (";"); \
break;
#define CASE_ZERO_ADDRESS(op, name) \
case NAME_TO_ID (op): \
__printf (name ";"); \
break;
#define CASE_THIS(op, lhs, equals, this) \
case NAME_TO_ID (op): \
dump_variable (opcode.data.op.lhs); \
__printf (" " equals " "); \
__printf (this ";"); \
break;
#define CASE_WITH(op, expr) \
case NAME_TO_ID (op): \
__printf ("with ("); \
dump_variable (opcode.data.op.expr); \
__printf (")"); \
break;
#define CASE_REG_VAR_DECL(op, min, max) \
case NAME_TO_ID (op): \
__printf ("var "); \
dump_variable (opcode.data.op.min); \
__printf (" .. "); \
dump_variable (opcode.data.op.max); \
__printf (";"); \
break;
static char *varg_end;
void
pp_opcode (opcode_counter_t oc, OPCODE opcode, bool is_rewrite)
{
uint8_t i = 1;
uint8_t opcode_num = opcode.op_idx;
__printf ("%03d: %20s ", oc, opcode_names[opcode_num]);
if (opcode_num != NAME_TO_ID (nop) && opcode_num != NAME_TO_ID (ret)
&& opcode_num != NAME_TO_ID (end_with))
{
for (i = 1; i < opcode_sizes[opcode_num]; i++)
__printf ("%4d ", ((uint8_t*)&opcode)[i]);
}
for (; i < 4; i++)
__printf (" ");
__printf (" // ");
switch (opcode_num)
{
CASE_CONDITIONAL_JUMP (is_true_jmp, "if (", value, ") goto", opcode)
CASE_CONDITIONAL_JUMP (is_false_jmp, "if (", value, " == false) goto", opcode)
CASE_UNCONDITIONAL_JUMP (jmp, "goto", 0, +, opcode_idx)
CASE_UNCONDITIONAL_JUMP (jmp_up, "goto", oc, -, opcode_count)
CASE_UNCONDITIONAL_JUMP (jmp_down, "goto", oc, +, opcode_count)
CASE_TRIPLE_ADDRESS (addition, dst, "=", var_left, "+", var_right)
CASE_TRIPLE_ADDRESS (substraction, dst, "=", var_left, "-", var_right)
CASE_TRIPLE_ADDRESS (division, dst, "=", var_left, "/", var_right)
CASE_TRIPLE_ADDRESS (multiplication, dst, "=", var_left, "*", var_right)
CASE_TRIPLE_ADDRESS (remainder, dst, "=", var_left, "%%", var_right)
CASE_TRIPLE_ADDRESS (b_shift_left, dst, "=", var_left, "<<", var_right)
CASE_TRIPLE_ADDRESS (b_shift_right, dst, "=", var_left, ">>", var_right)
CASE_TRIPLE_ADDRESS (b_shift_uright, dst, "=", var_left, ">>>", var_right)
CASE_TRIPLE_ADDRESS (b_and, dst, "=", var_left, "&", var_right)
CASE_TRIPLE_ADDRESS (b_or, dst, "=", var_left, "|", var_right)
CASE_TRIPLE_ADDRESS (b_xor, dst, "=", var_left, "^", var_right)
CASE_DOUBLE_ADDRESS (b_not, dst, "=", "~", var_right)
CASE_TRIPLE_ADDRESS (logical_and, dst, "=", var_left, "&&", var_right)
CASE_TRIPLE_ADDRESS (logical_or, dst, "=", var_left, "||", var_right)
CASE_DOUBLE_ADDRESS (logical_not, dst, "=", "!", var_right)
CASE_TRIPLE_ADDRESS (equal_value, dst, "=", var_left, "==", var_right)
CASE_TRIPLE_ADDRESS (not_equal_value, dst, "=", var_left, "!=", var_right)
CASE_TRIPLE_ADDRESS (equal_value_type, dst, "=", var_left, "===", var_right)
CASE_TRIPLE_ADDRESS (not_equal_value_type, dst, "=", var_left, "!==", var_right)
CASE_TRIPLE_ADDRESS (less_than, dst, "=", var_left, "<", var_right)
CASE_TRIPLE_ADDRESS (greater_than, dst, "=", var_left, ">", var_right)
CASE_TRIPLE_ADDRESS (less_or_equal_than, dst, "=", var_left, "<=", var_right)
CASE_TRIPLE_ADDRESS (greater_or_equal_than, dst, "=", var_left, ">=", var_right)
CASE_TRIPLE_ADDRESS (instanceof, dst, "=", var_left, "instanceof", var_right)
CASE_TRIPLE_ADDRESS (in, dst, "=", var_left, "in", var_right)
CASE_DOUBLE_ADDRESS_POST (post_incr, dst, "=", var_right, "++")
CASE_DOUBLE_ADDRESS_POST (post_decr, dst, "=", var_right, "--")
CASE_DOUBLE_ADDRESS (pre_incr, dst, "=", "++", var_right)
CASE_DOUBLE_ADDRESS (pre_decr, dst, "=", "--", var_right)
CASE_ASSIGNMENT (assignment, var_left, "=", value_right)
CASE_VARG_0_NAME_LHS (call_0, lhs, "=", "", name_lit_idx, "(", ")")
CASE_VARG_1_NAME_LHS (call_1, lhs, "=", "", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_N_NAME_LHS (call_n, lhs, "=", "", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_0_NAME_LHS (construct_0, lhs, "=", "new", name_lit_idx, "(", ")")
CASE_VARG_1_NAME_LHS (construct_1, lhs, "=", "new", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_N_NAME_LHS (construct_n, lhs, "=", "new", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_0_NAME (func_decl_0, "function", name_lit_idx, "(", ")")
CASE_VARG_1_NAME (func_decl_1, "function", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_2_NAME (func_decl_2, "function", name_lit_idx, "(", arg1_lit_idx, arg2_lit_idx, ")")
CASE_VARG_N_NAME (func_decl_n, "function", name_lit_idx, "(", arg1_lit_idx, arg2_lit_idx, ")")
CASE_VARG_0_NAME_LHS (func_expr_0, lhs, "=", "function", name_lit_idx, "(", ")")
CASE_VARG_1_NAME_LHS (func_expr_1, lhs, "=", "function", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_N_NAME_LHS (func_expr_n, lhs, "=", "function", name_lit_idx, "(", arg1_lit_idx, ")")
CASE_VARG_1_END (varg_1_end, arg1_lit_idx)
CASE_VARG_2_END (varg_2_end, arg1_lit_idx, arg2_lit_idx)
CASE_VARG_3_END (varg_3_end, arg1_lit_idx, arg2_lit_idx, arg3_lit_idx)
CASE_VARG_3 (varg_3, arg1_lit_idx, arg2_lit_idx, arg3_lit_idx);
CASE_EXIT (exitval, "exit", status_code)
CASE_SINGLE_ADDRESS (retval, "return", ret_value)
CASE_ZERO_ADDRESS (ret, "return")
CASE_ZERO_ADDRESS (end_with, "")
CASE_ZERO_ADDRESS (nop, "")
CASE_VARG_0_LHS (array_0, lhs, "=", "[", "]")
CASE_VARG_1_LHS (array_1, lhs, "=", "[", elem1, "]")
CASE_VARG_2_LHS (array_2, lhs, "=", "[", elem1, elem2, "]")
CASE_VARG_N_LHS (array_n, lhs, "=", "[", elem1, elem2, "]")
CASE_VARG_0_LHS (obj_0, lhs, "=", "{", "}")
CASE_VARG_1_LHS (obj_1, lhs, "=", "{", arg1, "}")
CASE_VARG_2_LHS (obj_2, lhs, "=", "{", arg1, arg2, "}")
CASE_VARG_N_LHS (obj_n, lhs, "=", "{", arg1, arg2, "}")
CASE_TRIPLE_ADDRESS (prop, lhs, "=", name, ":", value)
CASE_VARG_1_NAME_LHS (prop_access, lhs, "=", "", obj, "[", prop, "]")
CASE_VARG_0_NAME_LHS (prop_get_decl, lhs, "=", "get", prop, "[", "]")
CASE_VARG_1_NAME_LHS (prop_set_decl, lhs, "=", "set", prop, "[", arg, "]")
CASE_THIS (this, lhs, "=", "this")
CASE_DOUBLE_ADDRESS (delete, lhs, "=", "delete", obj)
CASE_DOUBLE_ADDRESS (typeof, lhs, "=", "typeof", obj)
CASE_WITH (with, expr)
CASE_VARG_0_NAME (var_decl, "var", variable_name, "", "")
CASE_REG_VAR_DECL (reg_var_decl, min, max)
}
if (is_rewrite)
__printf (" // REWRITE");
__printf ("\n");
}
+25
View File
@@ -0,0 +1,25 @@
/* 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 PRETTY_PRINTER
#define PRETTY_PRINTER
#include "interpreter.h"
void pp_opcode (opcode_counter_t, OPCODE, bool);
void pp_strings (const char **, uint8_t);
void pp_nums (const int32_t *, uint8_t, uint8_t);
#endif // PRETTY_PRINTER
+144
View File
@@ -0,0 +1,144 @@
/* 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 "serializer.h"
#include "parser.h"
#include "jerry-libc.h"
#include "bytecode-data.h"
#include "deserializer.h"
#include "pretty-printer.h"
static bool print_opcodes;
void
serializer_init (bool show_opcodes)
{
print_opcodes = show_opcodes;
}
uint16_t
serializer_dump_strings (const char *strings[], uint8_t size)
{
uint8_t i;
uint16_t offset = (uint16_t) (size * 2 + 1), res;
if (print_opcodes)
pp_strings (strings, size);
for (i = 0; i < size; i++)
{
offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
}
bytecode_data = mem_heap_alloc_block (offset, MEM_HEAP_ALLOC_SHORT_TERM);
res = offset;
bytecode_data[0] = size;
offset = (uint16_t) (size * 2 + 1);
for (i = 0; i < size; i++)
{
*((uint16_t *) (bytecode_data + i * 2 + 1)) = offset;
offset = (uint16_t) (offset + __strlen (strings[i]) + 1);
}
for (i = 0; i < size; i++)
{
offset = *((uint16_t *) (bytecode_data + i * 2 + 1));
__strncpy ((char *) (bytecode_data + offset), strings[i], __strlen (strings[i]) + 1);
}
#ifndef JERRY_NDEBUG
for (i = 0; i < size; i++)
{
JERRY_ASSERT (!__strcmp (strings[i], (const char *) deserialize_string_by_id (i)));
}
#endif
return res;
}
void
serializer_dump_nums (const int32_t nums[], uint8_t size, uint16_t offset, uint8_t strings_num)
{
uint8_t i, *data;
if (print_opcodes)
pp_nums (nums, size, strings_num);
data = mem_heap_alloc_block ((size_t) (offset + size * 4 + 1), MEM_HEAP_ALLOC_LONG_TERM);
if (!data)
parser_fatal (ERR_MEMORY);
__memcpy (data, bytecode_data, offset);
mem_heap_free_block (bytecode_data);
bytecode_data = data;
data += offset;
data[0] = size;
data++;
for (i = 0; i < size; i++)
{
__memcpy (data, nums + i, 4);
data += 4;
}
#ifndef JERRY_NDEBUG
for (i = 0; i < size; i++)
{
JERRY_ASSERT (nums[i] == deserialize_num_by_id ((uint8_t) (i + strings_num)));
}
JERRY_ASSERT (deserialize_min_temp () == (uint8_t) (size + strings_num));
#endif
}
static opcode_counter_t opcode_counter = 0;
void
serializer_dump_opcode (OPCODE opcode)
{
if (print_opcodes)
pp_opcode (opcode_counter, opcode, false);
JERRY_ASSERT( opcode_counter < MAX_OPCODES );
bytecode_opcodes[opcode_counter++] = opcode;
}
void
serializer_rewrite_opcode (const opcode_counter_t loc, OPCODE opcode)
{
JERRY_ASSERT( loc < MAX_OPCODES );
bytecode_opcodes[loc] = opcode;
if (print_opcodes)
{
pp_opcode (loc, opcode, true);
}
}
void
serializer_print_opcodes (void)
{
opcode_counter_t loc;
if (!print_opcodes)
return;
__printf ("AFTER OPTIMIZER:\n");
for (loc = 0; (*(uint32_t *) (bytecode_opcodes + loc) != 0x0); loc++)
{
pp_opcode (loc, bytecode_opcodes[loc], false);
}
}
+35
View File
@@ -0,0 +1,35 @@
/* 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 SERIALIZER_H
#define SERIALIZER_H
#include "globals.h"
#include "opcodes.h"
#include "interpreter.h"
void serializer_init (bool show_opcodes);
uint16_t serializer_dump_strings (const char **, uint8_t);
void serializer_dump_nums (const int32_t *, uint8_t, uint16_t, uint8_t);
void serializer_dump_opcode (OPCODE);
void serializer_rewrite_opcode (const opcode_counter_t, OPCODE);
void serializer_print_opcodes (void);
#endif // SERIALIZER_H