Improve line info construction (#4718)

- Simplify small encoding
- Better line info for some corner cases

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg 2021-07-16 16:08:38 +02:00 committed by GitHub
parent d5a7839632
commit 252d68936f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 64 deletions

View File

@ -62,35 +62,23 @@ static uint32_t
ecma_line_info_decode_small (uint8_t **buffer_p) /**< [in/out] target buffer */
{
uint8_t *source_p = *buffer_p;
uint32_t type = source_p[0];
*buffer_p = source_p + 1;
if (type < ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN)
{
*buffer_p = source_p + 1;
return type;
}
uint32_t value = source_p[1];
if (type == ECMA_LINE_INFO_ENCODE_TWO_BYTE)
{
*buffer_p = source_p + 2;
return (uint32_t) (value + ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN);
return ((uint32_t) source_p[1]) + ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN;
}
value |= ((uint32_t) source_p[2]) << 8;
if (type == ECMA_LINE_INFO_ENCODE_THREE_BYTE)
{
*buffer_p = source_p + 3;
return (uint32_t) (value + ECMA_LINE_INFO_ENCODE_THREE_BYTE_MIN);
}
JERRY_ASSERT (type == ECMA_LINE_INFO_ENCODE_FIVE_BYTE);
*buffer_p = source_p + 5;
return value | (((uint32_t) source_p[3]) << 8) | (((uint32_t) source_p[4]) << 8);
JERRY_ASSERT (type == ECMA_LINE_INFO_ENCODE_VLQ);
return ecma_line_info_decode_vlq (buffer_p) + ECMA_LINE_INFO_ENCODE_VLQ_MIN;
} /* ecma_line_info_decode_small */
/**

View File

@ -45,7 +45,7 @@
/**
* A default value for columns after a line update.
*/
#define ECMA_LINE_INFO_COLUMN_DEFAULT 126
#define ECMA_LINE_INFO_COLUMN_DEFAULT 127
/**
* Vlq encoding: flag which is set for all bytes except the last one.
@ -65,27 +65,22 @@
/**
* Small encoding: a value which represents a two byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE (UINT8_MAX - 2)
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE (UINT8_MAX - 1)
/**
* Small encoding: minimum value of an encoded two byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN (UINT8_MAX - 2)
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN (UINT8_MAX - 1)
/**
* Small encoding: a value which represents a three byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_THREE_BYTE (UINT8_MAX - 1)
#define ECMA_LINE_INFO_ENCODE_VLQ UINT8_MAX
/**
* Small encoding: minimum value of an encoded three byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_THREE_BYTE_MIN (ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN + UINT8_MAX + 1)
/**
* Small encoding: a value which represents a five byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_FIVE_BYTE UINT8_MAX
#define ECMA_LINE_INFO_ENCODE_VLQ_MIN (ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN + UINT8_MAX + 1)
/**
* Maximum number of line/column entries stored in a stream.

View File

@ -38,8 +38,9 @@
* The format is big endian.
*
* Small:
* First byte can encode signed values between 127 and -125 in 1 byte.
* Large values requires more bytes than vlq.
* One byte can encode signed values between 127 and -126.
* Two byte can encode signed values between 319 and -318.
* Large values are encoded with vlq with a prefix byte.
*
* The line-info data structure is a sequence of chunks:
*
@ -87,7 +88,7 @@
/**
* Maximum number of bytes requires to encode a number.
*/
#define PARSER_LINE_INFO_BUFFER_MAX_SIZE 5
#define PARSER_LINE_INFO_BUFFER_MAX_SIZE 6
/**
* Stream generation ends after this size is reached,
@ -160,41 +161,21 @@ static uint32_t
parser_line_info_encode_small (uint8_t *buffer_p, /**< target buffer */
uint32_t value) /**< encoded value */
{
if (value < ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN)
if (JERRY_LIKELY (value < ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN))
{
buffer_p[0] = (uint8_t) value;
return 1;
}
uint32_t length;
if (JERRY_LIKELY (value < ECMA_LINE_INFO_ENCODE_THREE_BYTE_MIN))
if (JERRY_LIKELY (value < ECMA_LINE_INFO_ENCODE_VLQ_MIN))
{
value -= ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN;
buffer_p[0] = ECMA_LINE_INFO_ENCODE_TWO_BYTE;
length = 2;
}
else
{
if (value <= (ECMA_LINE_INFO_ENCODE_THREE_BYTE_MIN + UINT16_MAX))
{
value -= ECMA_LINE_INFO_ENCODE_THREE_BYTE_MIN;
buffer_p[0] = ECMA_LINE_INFO_ENCODE_THREE_BYTE;
length = 3;
}
else
{
buffer_p[0] = ECMA_LINE_INFO_ENCODE_FIVE_BYTE;
buffer_p[3] = (uint8_t) (value >> 16);
buffer_p[4] = (uint8_t) (value >> 24);
length = 5;
}
buffer_p[2] = (uint8_t) (value >> 8);
buffer_p[1] = (uint8_t) (value - ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN);
return 2;
}
buffer_p[1] = (uint8_t) value;
return length;
*buffer_p++ = ECMA_LINE_INFO_ENCODE_VLQ;
return parser_line_info_encode_vlq (buffer_p, value - ECMA_LINE_INFO_ENCODE_VLQ_MIN) + 1;
} /* parser_line_info_encode_small */
/**

View File

@ -3339,6 +3339,13 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
{
if (context_p->status_flags & PARSER_IS_CLOSURE)
{
#if JERRY_LINE_INFO
if (context_p->line_info.first_page_p == NULL)
{
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
}
#endif /* JERRY_LINE_INFO */
parser_stack_pop_uint8 (context_p);
context_p->last_statement.current_p = NULL;
/* There is no lexer_next_token here, since the
@ -3501,6 +3508,13 @@ consume_last_statement:
{
parser_raise_error (context_p, PARSER_ERR_STATEMENT_EXPECTED);
}
#if JERRY_LINE_INFO
if (context_p->line_info.first_page_p == NULL)
{
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
}
#endif /* JERRY_LINE_INFO */
} /* parser_parse_statements */
/**

View File

@ -650,10 +650,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
if (context_p->line_info.first_page_p == NULL)
{
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
}
JERRY_ASSERT (context_p->line_info.first_page_p != NULL);
#endif /* JERRY_LINE_INFO */
JERRY_ASSERT (context_p->stack_depth == 0);
@ -2484,6 +2481,10 @@ parser_parse_arrow_function (parser_context_t *context_p, /**< context */
parser_raise_error (context_p, PARSER_ERR_NON_STRICT_ARG_DEFINITION);
}
#if JERRY_LINE_INFO
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
#endif /* JERRY_LINE_INFO */
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
@ -2600,12 +2601,13 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
scanner_seek (context_p);
}
context_p->source_end_p = range.source_end_p;
lexer_next_token (context_p);
#if JERRY_LINE_INFO
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
#endif /* JERRY_LINE_INFO */
context_p->source_end_p = range.source_end_p;
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
if (context_p->token.type != LEXER_EOS)
@ -2657,6 +2659,13 @@ parser_parse_class_fields (parser_context_t *context_p) /**< context */
context_p->source_end_p = source_end_p;
scanner_set_location (context_p, &end_location);
#if JERRY_LINE_INFO
if (context_p->line_info.first_page_p == NULL)
{
parser_line_info_append (context_p, context_p->token.line, context_p->token.column);
}
#endif /* JERRY_LINE_INFO */
compiled_code_p = parser_post_processing (context_p);
#if JERRY_PARSER_DUMP_BYTE_CODE

View File

@ -159,3 +159,13 @@ function f7() {
eval("assert(x()() === 5); function y() { return 5 } assert(x()() === 5)");
}
f7()
eval(" ");
eval("(function () {})")
try {
/* Only fails in ES5.1 */
eval("()=>0")
} catch (e) {
assert(e instanceof SyntaxError)
}

View File

@ -159,14 +159,14 @@ class_backtrace_callback (jerry_backtrace_frame_t *frame_p, /* frame information
{
TEST_ASSERT (jerry_backtrace_is_strict (frame_p));
TEST_ASSERT (location_p->line == 3);
TEST_ASSERT (location_p->column == 12);
TEST_ASSERT (location_p->column == 14);
return false;
}
TEST_ASSERT (frame_index == 2);
TEST_ASSERT (jerry_backtrace_is_strict (frame_p));
TEST_ASSERT (location_p->line == 2);
TEST_ASSERT (location_p->column == 5);
TEST_ASSERT (location_p->column == 7);
return false;
} /* class_backtrace_callback */