Rework storing the line/column/bytecode info (#4707)

This information is stored in a separate memory block instead
of being part of the byte code. Snapshot does not supported.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-07-15 13:45:10 +02:00
committed by GitHub
parent 4be05a74eb
commit 998e49a969
22 changed files with 1218 additions and 201 deletions
+45
View File
@@ -19,6 +19,7 @@
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-lcache.h"
#include "ecma-line-info.h"
#include "ecma-property-hashmap.h"
#include "jcontext.h"
#include "jrt-bit-fields.h"
@@ -1525,6 +1526,13 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
}
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
if (bytecode_p->status_flags & CBC_CODE_FLAGS_HAS_LINE_INFO)
{
ecma_line_info_free (ecma_compiled_code_get_line_info (bytecode_p));
}
#endif /* JERRY_LINE_INFO */
#if JERRY_DEBUGGER
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
&& !(bytecode_p->status_flags & CBC_CODE_FLAGS_DEBUGGER_IGNORE)
@@ -1657,6 +1665,43 @@ ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *b
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
/**
* Get the line info data from the byte code
*
* @return pointer to the line info data
*/
uint8_t *
ecma_compiled_code_get_line_info (const ecma_compiled_code_t *bytecode_header_p) /**< compiled code */
{
JERRY_ASSERT (bytecode_header_p != NULL);
JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_LINE_INFO);
ecma_value_t *base_p = ecma_compiled_code_resolve_arguments_start (bytecode_header_p);
#if JERRY_ESNEXT
if (CBC_FUNCTION_GET_TYPE (bytecode_header_p->status_flags) != CBC_FUNCTION_CONSTRUCTOR)
{
base_p--;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO)
{
base_p--;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS)
{
base_p--;
}
#endif /* JERRY_ESNEXT */
return ECMA_GET_INTERNAL_VALUE_POINTER (uint8_t, base_p[-1]);
} /* ecma_compiled_code_get_line_info */
#endif /* JERRY_LINE_INFO */
/**
* Get the resource name of a compiled code.
*
+3
View File
@@ -538,6 +538,9 @@ ecma_value_t *ecma_compiled_code_resolve_function_name (const ecma_compiled_code
uint32_t ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p);
ecma_collection_t *ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *bytecode_header_p);
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
uint8_t *ecma_compiled_code_get_line_info (const ecma_compiled_code_t *bytecode_header_p);
#endif /* JERRY_LINE_INFO */
ecma_value_t ecma_get_resource_name (const ecma_compiled_code_t *bytecode_p);
#if (JERRY_STACK_LIMIT != 0)
uintptr_t ecma_get_current_stack_usage (void);
+283
View File
@@ -0,0 +1,283 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 "ecma-helpers.h"
#include "ecma-line-info.h"
#if JERRY_LINE_INFO
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmalineinfo Line info
* @{
*/
/* The layout of the structure is defined in js-parser-line-info-create.c */
JERRY_STATIC_ASSERT ((ECMA_LINE_INFO_COLUMN_DEFAULT - 1) == ((ECMA_LINE_INFO_ENCODE_TWO_BYTE >> 1) - 1),
ecma_line_info_column_1_must_be_accessible_with_the_highest_one_byte_negative_value);
/**
* Decodes an uint32_t number, and updates the buffer position.
* Numbers expected to be larger values.
*
* @return the decoded value
*/
uint32_t
ecma_line_info_decode_vlq (uint8_t **buffer_p) /**< [in/out] target buffer */
{
uint8_t *source_p = *buffer_p;
uint32_t value = 0;
do
{
value = (value << ECMA_LINE_INFO_VLQ_SHIFT) | (*source_p & ECMA_LINE_INFO_VLQ_MASK);
}
while (*source_p++ & ECMA_LINE_INFO_VLQ_CONTINUE);
*buffer_p = source_p;
return value;
} /* ecma_line_info_decode_vlq */
/**
* Decodes an uint32_t number, and updates the buffer position.
* Numbers expected to be smaller values.
*
* @return the decoded value
*/
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];
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);
}
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);
} /* ecma_line_info_decode_small */
/**
* Updates a value using an encoded difference.
*
* @return updated value
*/
extern inline uint32_t JERRY_ATTR_ALWAYS_INLINE
ecma_line_info_difference_update (uint32_t current_value, /**< current value */
uint32_t difference_value) /**< encoded difference */
{
if ((difference_value & 0x1) == ECMA_LINE_INFO_INCREASE)
{
return current_value + (difference_value >> 1) + 1;
}
return current_value - (difference_value >> 1);
} /* ecma_line_info_difference_update */
/**
* Release line info data.
*/
void
ecma_line_info_free (uint8_t *line_info_p) /**< line info buffer */
{
uint8_t *source_p = line_info_p;
uint32_t total_length = ecma_line_info_decode_vlq (&source_p);
jmem_heap_free_block (line_info_p, total_length + (uint32_t) (source_p - line_info_p));
} /* ecma_line_info_free */
/**
* Returns the line/column information for a given byte code offset.
*/
void
ecma_line_info_get (uint8_t *line_info_p, /**< line info buffer */
uint32_t offset, /**< byte code offset */
jerry_backtrace_location_t *location_p) /**< [out] location */
{
uint32_t line = 1;
uint32_t column = ECMA_LINE_INFO_COLUMN_DEFAULT;
uint32_t end_offset = 0;
uint32_t end_offset_increase;
uint32_t value;
/* Skip total_length. */
ecma_line_info_decode_vlq (&line_info_p);
while (true)
{
value = ecma_line_info_decode_vlq (&line_info_p);
line = ecma_line_info_difference_update (line, value);
if (*line_info_p == 0)
{
break;
}
uint8_t *size_p = line_info_p + *line_info_p + (ECMA_LINE_INFO_STREAM_SIZE_MIN + 1);
end_offset += ecma_line_info_decode_vlq (&size_p);
if (offset < end_offset)
{
break;
}
line_info_p = size_p;
}
line_info_p++;
do
{
end_offset_increase = ecma_line_info_decode_small (&line_info_p);
if (end_offset_increase & ECMA_LINE_INFO_HAS_LINE)
{
value = ecma_line_info_decode_small (&line_info_p);
line = ecma_line_info_difference_update (line, value);
column = ECMA_LINE_INFO_COLUMN_DEFAULT;
}
end_offset_increase >>= 1;
value = ecma_line_info_decode_small (&line_info_p);
column = ecma_line_info_difference_update (column, value);
end_offset += end_offset_increase;
}
while (end_offset_increase != 0 && end_offset <= offset);
location_p->line = line;
location_p->column = column;
} /* ecma_line_info_get */
#if JERRY_PARSER_DUMP_BYTE_CODE
/**
* Dumps line info data.
*/
void
ecma_line_info_dump (uint8_t *line_info_p) /**< dumps line info data */
{
bool block_last = false;
uint32_t block_line = 1;
uint32_t block_byte_code_offset = 0;
uint32_t value;
value = ecma_line_info_decode_vlq (&line_info_p);
JERRY_DEBUG_MSG ("\nLine info size: %d bytes\n", (int) value);
while (true)
{
value = ecma_line_info_decode_vlq (&line_info_p);
block_line = ecma_line_info_difference_update (block_line, value);
JERRY_DEBUG_MSG ("\nNew block: line: %d", (int) block_line);
if (*line_info_p == 0)
{
JERRY_DEBUG_MSG (" StreamLength: [last]\n");
block_last = true;
}
else
{
uint8_t *size_p = line_info_p + *line_info_p + (ECMA_LINE_INFO_STREAM_SIZE_MIN + 1);
value = ecma_line_info_decode_vlq (&size_p);
JERRY_DEBUG_MSG (" StreamLength: %d ByteCodeSize: %d\n",
(int) (*line_info_p + ECMA_LINE_INFO_STREAM_SIZE_MIN),
(int) value);
}
line_info_p++;
uint32_t stream_line = block_line;
uint32_t stream_column = ECMA_LINE_INFO_COLUMN_DEFAULT;
uint32_t stream_end_offset = block_byte_code_offset;
while (true)
{
uint32_t stream_end_offset_increase = ecma_line_info_decode_small (&line_info_p);
if (stream_end_offset_increase & ECMA_LINE_INFO_HAS_LINE)
{
value = ecma_line_info_decode_small (&line_info_p);
stream_line = ecma_line_info_difference_update (stream_line, value);
stream_column = ECMA_LINE_INFO_COLUMN_DEFAULT;
}
stream_end_offset_increase >>= 1;
value = ecma_line_info_decode_small (&line_info_p);
stream_column = ecma_line_info_difference_update (stream_column, value);
if (stream_end_offset_increase == 0)
{
JERRY_DEBUG_MSG (" ByteCodeEndOffset: [unterminated] Line: %d Column: %d\n",
(int) stream_line,
(int) stream_column);
break;
}
stream_end_offset += stream_end_offset_increase;
JERRY_DEBUG_MSG (" ByteCodeEndOffset: %d Line: %d Column: %d\n",
(int) stream_end_offset,
(int) stream_line,
(int) stream_column);
}
if (block_last)
{
break;
}
block_byte_code_offset += ecma_line_info_decode_vlq (&line_info_p);
}
} /* ecma_line_info_dump */
#endif /* JERRY_PARSER_DUMP_BYTE_CODE */
#endif /* JERRY_LINE_INFO */
/**
* @}
* @}
*/
+121
View File
@@ -0,0 +1,121 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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 ECMA_LINE_INFO_H
#define ECMA_LINE_INFO_H
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmalineinfo Line info
* @{
*/
#if JERRY_LINE_INFO
#include "ecma-globals.h"
/**
* Increase the current value of line or column.
*/
#define ECMA_LINE_INFO_INCREASE 0x0
/**
* Decrease the current value of line or column.
*/
#define ECMA_LINE_INFO_DECREASE 0x1
/**
* Line update is present.
*/
#define ECMA_LINE_INFO_HAS_LINE 0x1
/**
* A default value for columns after a line update.
*/
#define ECMA_LINE_INFO_COLUMN_DEFAULT 126
/**
* Vlq encoding: flag which is set for all bytes except the last one.
*/
#define ECMA_LINE_INFO_VLQ_CONTINUE 0x80
/**
* Vlq encoding: mask to decode the number fragment.
*/
#define ECMA_LINE_INFO_VLQ_MASK 0x7f
/**
* Vlq encoding: number of bits stored in a byte.
*/
#define ECMA_LINE_INFO_VLQ_SHIFT 7
/**
* Small encoding: a value which represents a two byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE (UINT8_MAX - 2)
/**
* Small encoding: minimum value of an encoded two byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_TWO_BYTE_MIN (UINT8_MAX - 2)
/**
* Small encoding: a value which represents a three byte long number.
*/
#define ECMA_LINE_INFO_ENCODE_THREE_BYTE (UINT8_MAX - 1)
/**
* 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
/**
* Maximum number of line/column entries stored in a stream.
*/
#define ECMA_LINE_INFO_STREAM_VALUE_COUNT_MAX 48
/**
* Minimum size of a stream (except the last one).
*/
#define ECMA_LINE_INFO_STREAM_SIZE_MIN \
((2 * ECMA_LINE_INFO_STREAM_VALUE_COUNT_MAX) - 1)
/* Helper functions for parser/js/js-parser-line-info-create.c. */
uint32_t ecma_line_info_decode_vlq (uint8_t **buffer_p);
uint32_t ecma_line_info_difference_update (uint32_t current_value, uint32_t difference_value);
/* General functions. */
void ecma_line_info_free (uint8_t *line_info_p);
void ecma_line_info_get (uint8_t *line_info_p, uint32_t offset,
jerry_backtrace_location_t *location_p);
#if JERRY_PARSER_DUMP_BYTE_CODE
void ecma_line_info_dump (uint8_t *line_info_p);
#endif /* JERRY_PARSER_DUMP_BYTE_CODE */
#endif /* ECMA_LINE_INFO_H */
/**
* @}
* @}
*/
#endif /* !ECMA_LINE_INFO_H */