From ed4e2a20e4395d75394c44edb62157490bd655c8 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Wed, 23 Jul 2014 22:55:45 +0400 Subject: [PATCH] Add deserializer --- Makefile.mak | 2 +- src/libruntime/deserializer.h | 25 ++++++++ src/libruntime/target/linux/bytecode-linux.h | 37 ++++++++++++ src/libruntime/target/linux/deserializer.c | 61 ++++++++++++++++++++ src/libruntime/target/linux/serializer.c | 41 +++++++++++-- src/libruntime/target/stm32f4/deserializer.c | 36 ++++++++++++ src/main.c | 6 +- 7 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 src/libruntime/deserializer.h create mode 100644 src/libruntime/target/linux/bytecode-linux.h create mode 100644 src/libruntime/target/linux/deserializer.c create mode 100644 src/libruntime/target/stm32f4/deserializer.c diff --git a/Makefile.mak b/Makefile.mak index bacd235da..9cdab32c6 100644 --- a/Makefile.mak +++ b/Makefile.mak @@ -163,7 +163,7 @@ SOURCES_JERRY = \ $(wildcard ./src/libecmaoperations/*.c) \ $(wildcard ./src/liballocator/*.c) \ $(wildcard ./src/libcoreint/*.c) ) \ - $(wildcard src/libruntime/target/$(TARGET_SYSTEM)/*) + $(wildcard src/libruntime/target/$(TARGET_SYSTEM)/*.c) INCLUDES_JERRY = \ -I src \ diff --git a/src/libruntime/deserializer.h b/src/libruntime/deserializer.h new file mode 100644 index 000000000..a83fe52db --- /dev/null +++ b/src/libruntime/deserializer.h @@ -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 DESERIALIZER_H +#define DESERIALIZER_H + +#include "globals.h" + +const char *deserializer_get_string_by_id (uint8_t); +int deserializer_get_num_by_id (uint8_t); +const void *deserializer_get_bytecode (void); + +#endif //DESERIALIZER_H \ No newline at end of file diff --git a/src/libruntime/target/linux/bytecode-linux.h b/src/libruntime/target/linux/bytecode-linux.h new file mode 100644 index 000000000..2c7a4c653 --- /dev/null +++ b/src/libruntime/target/linux/bytecode-linux.h @@ -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 \ No newline at end of file diff --git a/src/libruntime/target/linux/deserializer.c b/src/libruntime/target/linux/deserializer.c new file mode 100644 index 000000000..78d3ec111 --- /dev/null +++ b/src/libruntime/target/linux/deserializer.c @@ -0,0 +1,61 @@ +/* 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-linux.h" + +const char * +deserializer_get_string_by_id (uint8_t id) +{ + uint8_t size = *bytecode_data, *data = bytecode_data, offset; + + if (id >= size) + return NULL; + + data += id + 1; + + offset = *data; + + return (char*) (bytecode_data + offset); +} + +int +deserializer_get_num_by_id (uint8_t id) +{ + int *num_data; + uint8_t str_size, str_offset, *data, num_size; + + str_size = *bytecode_data; + data = bytecode_data + str_size; + str_offset = *data; + data = bytecode_data + str_offset; + + while (*data) + data++; + + num_data = (int *) ++data; + num_size = *data; + + if (id >= num_size) + return 0; + + return num_data[id]; +} + +const void * +deserializer_get_bytecode (void) +{ + return bytecode_opcodes; +} diff --git a/src/libruntime/target/linux/serializer.c b/src/libruntime/target/linux/serializer.c index c2abf59d1..f871053da 100644 --- a/src/libruntime/target/linux/serializer.c +++ b/src/libruntime/target/linux/serializer.c @@ -15,7 +15,7 @@ #include "serializer.h" #include "jerry-libc.h" -#include "opcodes.h" +#include "bytecode-linux.h" _FILE *dump; @@ -44,24 +44,39 @@ uint8_t serializer_dump_strings (const char *strings[], uint8_t size) { uint8_t i; - uint8_t offset = size; + uint8_t offset = (uint8_t) (size + 1), res; __printf ("STRINGS %d:\n", size); for (i = 0; i < size; i++) { __printf ("%3d %3d %20s\n", i, offset, strings[i]); - offset = (uint8_t ) (offset + __strlen (strings[i]) + 1); + offset = (uint8_t) (offset + __strlen (strings[i]) + 1); } - return offset; + bytecode_data = mem_heap_alloc_block (offset, MEM_HEAP_ALLOC_SHORT_TERM); + res = offset; + + bytecode_data[0] = size; + offset = (uint8_t) (size + 1); + for (i = 0; i < size; i++) + { + bytecode_data[i + 1] = offset; + offset = (uint8_t) (offset + __strlen (strings[i]) + 1); + } + + for (i = 0; i < size; i++) + __strncpy ((void *) (bytecode_data + bytecode_data[i + 1]), strings[i], __strlen (strings[i]) + 1); + + return res; } void serializer_dump_nums (const int nums[], uint8_t size, uint8_t offset, uint8_t strings_num) { - uint8_t i; + uint8_t i, *data; + JERRY_STATIC_ASSERT (sizeof (int) == 4); - offset = (uint8_t) (offset + size); + offset = (uint8_t) (offset + size + 1); __printf ("NUMS %d:\n", size); for (i = 0; i < size; i++) { @@ -70,6 +85,18 @@ serializer_dump_nums (const int nums[], uint8_t size, uint8_t offset, uint8_t st } __printf ("\n"); + + data = mem_heap_alloc_block ((size_t) (offset + size * 4), MEM_HEAP_ALLOC_LONG_TERM); + 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; + } } static int opcode_counter = 0; @@ -80,6 +107,7 @@ serializer_dump_opcode (const void *opcode) uint8_t i; int opcode_num = (int)((char*)opcode)[0]; + bytecode_opcodes[opcode_counter] = *((OPCODE*)opcode); __printf ("%03d: %20s ", opcode_counter++, opcode_names[opcode_num]); for (i = 1; i < opcode_sizes[opcode_num]; i++) __printf ("%4d ", ((char*)opcode)[i]); @@ -93,6 +121,7 @@ serializer_rewrite_opcode (const uint8_t loc, const void *opcode) uint8_t i; int opcode_num = (int)((char*)opcode)[0]; + bytecode_opcodes[loc] = *((OPCODE*)opcode); __printf ("%03d: %20s ", loc, opcode_names[opcode_num]); for (i = 1; i < opcode_sizes[opcode_num]; i++) __printf ("%4d ", ((char*)opcode)[i]); diff --git a/src/libruntime/target/stm32f4/deserializer.c b/src/libruntime/target/stm32f4/deserializer.c new file mode 100644 index 000000000..6ddc3f6a3 --- /dev/null +++ b/src/libruntime/target/stm32f4/deserializer.c @@ -0,0 +1,36 @@ +/* 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" + +TODO (Implement) + +const char * +deserializer_get_string_by_id (uint8_t id) +{ + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (id); +} + +int +deserializer_get_num_by_id (uint8_t id) +{ + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (id); +} + +const void * +deserializer_get_bytecode (void) +{ + JERRY_UNIMPLEMENTED (); +} diff --git a/src/main.c b/src/main.c index b7c848a4d..1db58e15e 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ #include "lexer.h" #include "parser.h" #include "serializer.h" +#include "deserializer.h" #define MAX_STRINGS 100 #define MAX_NUMS 25 @@ -67,9 +68,8 @@ jerry_run( const char *script_source, parser_init (); parser_parse_program (); - //gen_bytecode (generated_source); - //gen_bytecode (); - //run_int (); + init_int (deserializer_get_bytecode ()); + run_int (); } /* jerry_run */ #ifdef __HOST