Add literals - replacements of strings and numbers in parser.

This commit is contained in:
Ilmir Usmanov
2014-10-31 21:22:52 +04:00
parent 05cf2dbe04
commit 8c7dc08d93
26 changed files with 822 additions and 568 deletions
+3 -5
View File
@@ -19,7 +19,7 @@
#include "opcodes.h"
#include "stack.h"
#include "jerry-libc.h"
#include "lp-string.h"
#include "literal.h"
#include "scopes-tree.h"
/* bytecode_data contains identifiers, string and num literals.
@@ -35,11 +35,9 @@
} */
struct
{
const lp_string *strings;
const ecma_number_t *nums;
const literal *literals;
const opcode_t *opcodes;
uint8_t strs_count;
uint8_t nums_count;
uint8_t literals_count;
opcode_counter_t opcodes_count;
}
bytecode_data;
+16 -22
View File
@@ -15,26 +15,21 @@
#include "deserializer.h"
#include "bytecode-data.h"
#include "ecma-helpers.h"
const ecma_char_t *
deserialize_string_by_id (uint8_t id)
const ecma_char_t *strings_buffer;
void
deserializer_set_strings_buffer (const ecma_char_t *s)
{
JERRY_ASSERT (id < bytecode_data.strs_count);
JERRY_ASSERT (bytecode_data.strings[id].str[bytecode_data.strings[id].length] == '\0');
return ((const ecma_char_t *) bytecode_data.strings[id].str);
strings_buffer = s;
}
ecma_number_t
deserialize_num_by_id (uint8_t id)
literal
deserialize_literal_by_id (uint8_t id)
{
JERRY_ASSERT (id >= bytecode_data.strs_count);
id = (uint8_t) (id - bytecode_data.strs_count);
JERRY_ASSERT (id < bytecode_data.nums_count);
return bytecode_data.nums[id];
JERRY_ASSERT (id < bytecode_data.literals_count);
return bytecode_data.literals[id];
}
const void *
@@ -58,25 +53,24 @@ deserialize_opcode (opcode_counter_t oc)
uint8_t
deserialize_min_temp (void)
{
return (uint8_t) (bytecode_data.strs_count + bytecode_data.nums_count);
return bytecode_data.literals_count;
}
void
deserializer_init (void)
{
bytecode_data.strings = NULL;
bytecode_data.nums = NULL;
bytecode_data.literals = NULL;
strings_buffer = NULL;
bytecode_data.opcodes = NULL;
}
void
deserializer_free (void)
{
if (bytecode_data.strs_count > 0)
if (strings_buffer)
{
mem_heap_free_block ((uint8_t *) bytecode_data.strings[0].str);
mem_heap_free_block ((uint8_t *) strings_buffer);
}
mem_heap_free_block ((uint8_t *) bytecode_data.strings);
mem_heap_free_block ((uint8_t *) bytecode_data.nums);
mem_heap_free_block ((uint8_t *) bytecode_data.literals);
mem_heap_free_block ((uint8_t *) bytecode_data.opcodes);
}
+3 -2
View File
@@ -19,10 +19,11 @@
#include "globals.h"
#include "ecma-globals.h"
#include "opcodes.h"
#include "literal.h"
void deserializer_init (void);
const ecma_char_t *deserialize_string_by_id (uint8_t);
ecma_number_t deserialize_num_by_id (uint8_t);
void deserializer_set_strings_buffer (const ecma_char_t *);
literal deserialize_literal_by_id (uint8_t);
const void *deserialize_bytecode (void);
opcode_t deserialize_opcode (opcode_counter_t);
uint8_t deserialize_min_temp (void);
+35 -38
View File
@@ -20,6 +20,7 @@
#include "lexer.h"
#include "deserializer.h"
#include "opcodes-native-call.h"
#include "ecma-helpers.h"
#include <stdarg.h>
#define NAME_TO_ID(op) (__op__idx_##op)
@@ -45,43 +46,48 @@ static uint8_t opcode_sizes[] =
};
static void
dump_lp (lp_string lp)
dump_literal (literal lit)
{
for (ecma_length_t i = 0; i < lp.length; i++)
switch (lit.type)
{
__putchar (lp.str[i]);
case LIT_NUMBER:
{
__printf ("%d : NUMBER", lit.data.num);
break;
}
case LIT_MAGIC_STR:
{
__printf ("%s : MAGIC STRING", (const char *) ecma_get_magic_string_zt (lit.data.magic_str_id));
break;
}
case LIT_STR:
{
__printf ("%s : STRING", (const char *) (lit.data.lp.str));
break;
}
default:
{
JERRY_UNREACHABLE ();
}
}
}
void
pp_strings (const lp_string strings[], uint8_t size)
pp_literals (const literal lits[], uint8_t size)
{
__printf ("STRINGS %d:\n", size);
__printf ("LITERALS %d:\n", size);
for (uint8_t i = 0; i < size; i++)
{
__printf ("%3d ", i);
dump_lp (strings[i]);
dump_literal (lits[i]);
__putchar ('\n');
}
}
void
pp_nums (const ecma_number_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, (int) nums[i]);
}
__printf ("\n");
}
static const char *
var_id_to_string (char *res, idx_t id)
{
if (id >= lexer_get_reserved_ids_count ())
if (id >= lexer_get_literals_count ())
{
__strncpy (res, "tmp", 3);
if (id / 100 != 0)
@@ -103,28 +109,19 @@ var_id_to_string (char *res, idx_t id)
return res;
}
}
else if (id < lexer_get_strings_count ())
literal lit = lexer_get_literal_by_id (id);
if (lit.type == LIT_STR || lit.type == LIT_MAGIC_STR)
{
lp_string str = lexer_get_string_by_id (id);
__strncpy (res, (char *) str.str, str.length);
return (char *) literal_to_zt (lit);
}
else if (lit.type == LIT_NUMBER)
{
ecma_number_to_zt_string (lit.data.num, (ecma_char_t *) res, ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER);
return res;
}
else
{
int i = 0;
int num = (int) lexer_get_num_by_id (id);
int temp = num;
for (; temp != 0; i++)
{
temp /= 10;
}
do
{
res[i--] = (char) (num % 10 + '0');
num /= 10;
}
while (i >= 0);
return res;
JERRY_UNREACHABLE ();
}
}
@@ -152,7 +149,7 @@ pp_printf (const char *format, ...)
}
case 's':
{
char res[32] = {'\0'};
char res[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER] = {'\0'};
__printf ("%s", var_id_to_string (res, (idx_t) va_arg (args, int)));
break;
}
+2 -3
View File
@@ -19,11 +19,10 @@
#include "globals.h"
#ifdef JERRY_ENABLE_PP
#include "interpreter.h"
#include "lp-string.h"
#include "literal.h"
void pp_opcode (opcode_counter_t, opcode_t, bool);
void pp_strings (const lp_string *, uint8_t);
void pp_nums (const ecma_number_t *, uint8_t, uint8_t);
void pp_literals (const literal *, uint8_t);
#endif // JERRY_ENABLE_PP
#endif // PRETTY_PRINTER
+4 -8
View File
@@ -37,21 +37,17 @@ serializer_merge_scopes_into_bytecode (void)
void
serializer_dump_strings_and_nums (const lp_string strings[], uint8_t strs_count,
const ecma_number_t nums[], uint8_t nums_count)
serializer_dump_literals (const literal literals[], uint8_t literals_count)
{
#ifdef JERRY_ENABLE_PP
if (print_opcodes)
{
pp_strings (strings, strs_count);
pp_nums (nums, nums_count, strs_count);
pp_literals (literals, literals_count);
}
#endif
bytecode_data.strs_count = strs_count;
bytecode_data.nums_count = nums_count;
bytecode_data.strings = strings;
bytecode_data.nums = nums;
bytecode_data.literals_count = literals_count;
bytecode_data.literals = literals;
}
void
+2 -3
View File
@@ -19,12 +19,11 @@
#include "globals.h"
#include "opcodes.h"
#include "interpreter.h"
#include "lp-string.h"
#include "literal.h"
#include "scopes-tree.h"
void serializer_init (bool show_opcodes);
void serializer_dump_strings_and_nums (const lp_string *, uint8_t,
const ecma_number_t *, uint8_t);
void serializer_dump_literals (const literal *, uint8_t);
void serializer_set_scope (scopes_tree);
void serializer_merge_scopes_into_bytecode (void);
void serializer_dump_opcode (opcode_t);