Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_bytecode Bytecode
* @{
*/
#define CBC_OPCODE(arg1, arg2, arg3, arg4) \
((arg2) | (((arg3) + CBC_STACK_ADJUST_BASE) << CBC_STACK_ADJUST_SHIFT)),
/**
* Flags of the opcodes.
*/
const uint8_t cbc_flags[] JERRY_CONST_DATA =
{
CBC_OPCODE_LIST
};
/**
* Flags of the extended opcodes.
*/
const uint8_t cbc_ext_flags[] =
{
CBC_EXT_OPCODE_LIST
};
#undef CBC_OPCODE
#ifdef PARSER_DUMP_BYTE_CODE
#define CBC_OPCODE(arg1, arg2, arg3, arg4) #arg1,
/**
* Names of the opcodes.
*/
const char * const cbc_names[] =
{
CBC_OPCODE_LIST
};
/**
* Names of the extended opcodes.
*/
const char * const cbc_ext_names[] =
{
CBC_EXT_OPCODE_LIST
};
#undef CBC_OPCODE
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/

View file

@ -0,0 +1,688 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 BYTE_CODE_H
#define BYTE_CODE_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_bytecode Bytecode
* @{
*/
/**
* Compact byte code (CBC) is a byte code representation
* of EcmaScript which is designed for low memory
* environments. Most opcodes are only one or sometimes
* two byte long so the CBC provides a small binary size.
*
* The execution engine of CBC is a stack machine, where
* the maximum stack size is known in advance for each
* function.
*/
/**
* Byte code flags. Only the lower 5 bit can be used
* since the stack change is encoded in the upper
* three bits for each instruction between -4 and 3
* (except for call / construct opcodes).
*/
#define CBC_STACK_ADJUST_BASE 4
#define CBC_STACK_ADJUST_SHIFT 5
#define CBC_STACK_ADJUST_VALUE(value) \
(((value) >> CBC_STACK_ADJUST_SHIFT) - CBC_STACK_ADJUST_BASE)
#define CBC_NO_FLAG 0x00u
#define CBC_HAS_LITERAL_ARG 0x01u
#define CBC_HAS_LITERAL_ARG2 0x02u
#define CBC_HAS_BYTE_ARG 0x04u
#define CBC_HAS_BRANCH_ARG 0x08u
/* These flags are shared */
#define CBC_FORWARD_BRANCH_ARG 0x10u
#define CBC_POP_STACK_BYTE_ARG 0x10u
#define CBC_ARG_TYPES (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2 | CBC_HAS_BYTE_ARG | CBC_HAS_BRANCH_ARG)
#define CBC_HAS_POP_STACK_BYTE_ARG (CBC_HAS_BYTE_ARG | CBC_POP_STACK_BYTE_ARG)
/* Debug macro. */
#define CBC_ARGS_EQ(op, types) \
((cbc_flags[op] & CBC_ARG_TYPES) == (types))
/* Debug macro. */
#define CBC_SAME_ARGS(op1, op2) \
((cbc_flags[op1] & CBC_ARG_TYPES) == (cbc_flags[op2] & CBC_ARG_TYPES))
#define CBC_UNARY_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, 0, \
(VM_OC_ ## group) | VM_OC_GET_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _LITERAL, CBC_HAS_LITERAL_ARG, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_STACK)
#define CBC_BINARY_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -1, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _RIGHT_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _TWO_LITERALS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK)
#define CBC_UNARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -2, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (name ## _PUSH_RESULT, CBC_NO_FLAG, -1, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _BLOCK, CBC_NO_FLAG, -2, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (name ## _IDENT, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT) \
CBC_OPCODE (name ## _IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _IDENT_BLOCK, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK)
#define CBC_BINARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -4, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (name ## _LITERAL, CBC_HAS_LITERAL_ARG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE) \
#define CBC_EXT_BINARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name ## _PUSH_RESULT, CBC_NO_FLAG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, -2, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
#define CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION(name, group) \
CBC_OPCODE (name ## _BLOCK, CBC_NO_FLAG, -4, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (name ## _LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
#define CBC_UNARY_LVALUE_WITH_IDENT 3
#define CBC_BINARY_LVALUE_WITH_LITERAL 1
#define CBC_BINARY_WITH_LITERAL 1
#define CBC_BINARY_WITH_TWO_LITERALS 2
/**
* Several opcodes (mostly call and assignment opcodes) have
* two forms: one which does not push a return value onto
* the stack, and another which does. The reasion is that
* the return value of these opcodes are often not used
* and the first form provides smaller byte code.
*
* The following rules must be kept by the code generator:
* - only the opcode without return value can be emitted
* by the code generator
* - the first form can be converted to the second form
* by adding 1 to the opcode
* - after the conversion the opcode must be immediately
* flushed, so no further changes are possible
*
* Hence CBC_NO_RESULT_OPERATION (context_p->last_cbc_opcode)
* cannot be true for an opcode which has a result
*/
#define CBC_NO_RESULT_OPERATION(opcode) \
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_END)
#define CBC_NO_RESULT_BLOCK(opcode) \
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_ASSIGN_ADD)
#define CBC_NO_RESULT_COMPOUND_ASSIGMENT(opcode) \
((opcode) >= CBC_ASSIGN_ADD && (opcode) < CBC_END)
/**
* Branch instructions are organized in group of 8 opcodes.
* - 1st opcode: unused, can be used for other purpose
* - 2nd opcode: forward branch with 1 byte offset
* - 3rd opcode: forward branch with 2 byte offset
* - 4th opcode: forward branch with 3 byte offset
* - 5th opcode: unused, can be used for other purpose
* - 6th opcode: backward branch with 1 byte offset
* - 7th opcode: backward branch with 2 byte offset
* - 8th opcode: backward branch with 3 byte offset
*
* Reasons:
* The branch_opcode & 0x3 tells the length in bytes of the offset
* If branch offset & 0x4 == 0, it is a forward branch. Otherwise
* it is backward.
*
* The offset bytes are encoded in higher to lower order.
*/
#define CBC_FORWARD_BRANCH(name, stack, vm_oc) \
CBC_OPCODE (name, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH) \
CBC_OPCODE (name ## _2, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH) \
CBC_OPCODE (name ## _3, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH)
#define CBC_BACKWARD_BRANCH(name, stack, vm_oc) \
CBC_OPCODE (name, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH) \
CBC_OPCODE (name ## _2, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH) \
CBC_OPCODE (name ## _3, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH)
#define CBC_BRANCH_OFFSET_LENGTH(opcode) \
((opcode) & 0x3)
#define CBC_BRANCH_IS_BACKWARD(flags) \
(!((flags) & CBC_FORWARD_BRANCH_ARG))
#define CBC_BRANCH_IS_FORWARD(flags) \
((flags) & CBC_FORWARD_BRANCH_ARG)
/* Stack consumption of opcodes with context. */
/* PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION must be <= 4 */
#define PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION 3
/* PARSER_WITH_CONTEXT_STACK_ALLOCATION must be <= 4 */
#define PARSER_WITH_CONTEXT_STACK_ALLOCATION 2
/* PARSER_TRY_CONTEXT_STACK_ALLOCATION must be <= 3 */
#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 2
/**
* Opcode definitions.
*/
#define CBC_OPCODE_LIST \
/* Branch opcodes first. Some other opcodes are mixed. */ \
CBC_OPCODE (CBC_EXT_OPCODE, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_FORWARD_BRANCH (CBC_JUMP_FORWARD, 0, \
VM_OC_JUMP) \
CBC_OPCODE (CBC_POP, CBC_NO_FLAG, -1, \
VM_OC_POP) \
CBC_BACKWARD_BRANCH (CBC_JUMP_BACKWARD, 0, \
VM_OC_JUMP) \
CBC_OPCODE (CBC_POP_BLOCK, CBC_NO_FLAG, -1, \
VM_OC_POP_BLOCK | VM_OC_PUT_BLOCK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_TRUE_FORWARD, -1, \
VM_OC_BRANCH_IF_TRUE) \
CBC_OPCODE (CBC_THROW, CBC_NO_FLAG, -1, \
VM_OC_THROW | VM_OC_GET_STACK) \
CBC_BACKWARD_BRANCH (CBC_BRANCH_IF_TRUE_BACKWARD, -1, \
VM_OC_BRANCH_IF_TRUE) \
CBC_OPCODE (CBC_CONTEXT_END, CBC_NO_FLAG, 0, \
VM_OC_CONTEXT_END) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_FALSE_FORWARD, -1, \
VM_OC_BRANCH_IF_FALSE) \
CBC_OPCODE (CBC_CREATE_OBJECT, CBC_NO_FLAG, 1, \
VM_OC_PUSH_OBJECT | VM_OC_PUT_STACK) \
CBC_BACKWARD_BRANCH (CBC_BRANCH_IF_FALSE_BACKWARD, -1, \
VM_OC_BRANCH_IF_FALSE) \
CBC_OPCODE (CBC_SET_PROPERTY, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_PROPERTY | VM_OC_GET_STACK_LITERAL) \
CBC_FORWARD_BRANCH (CBC_JUMP_FORWARD_EXIT_CONTEXT, 0, \
VM_OC_JUMP_AND_EXIT_CONTEXT) \
CBC_OPCODE (CBC_CREATE_ARRAY, CBC_NO_FLAG, 1, \
VM_OC_PUSH_ARRAY | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_LOGICAL_TRUE, -1, \
VM_OC_BRANCH_IF_LOGICAL_TRUE) \
CBC_OPCODE (CBC_ARRAY_APPEND, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_APPEND_ARRAY) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_LOGICAL_FALSE, -1, \
VM_OC_BRANCH_IF_LOGICAL_FALSE) \
CBC_OPCODE (CBC_PUSH_ELISION, CBC_NO_FLAG, 1, \
VM_OC_PUSH_ELISON | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_STRICT_EQUAL, -1, \
VM_OC_BRANCH_IF_STRICT_EQUAL) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_PUSH_LITERAL, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_PUSH | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_PUSH_TWO_LITERALS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 2, \
VM_OC_PUSH_TWO | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_PUSH_THREE_LITERALS, CBC_HAS_LITERAL_ARG2, 3, \
VM_OC_PUSH_THREE | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_PUSH_UNDEFINED, CBC_NO_FLAG, 1, \
VM_OC_PUSH_UNDEFINED | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_TRUE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_TRUE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_FALSE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_FALSE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NULL, CBC_NO_FLAG, 1, \
VM_OC_PUSH_NULL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_THIS, CBC_NO_FLAG, 1, \
VM_OC_PUSH_THIS | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_THIS_LITERAL, CBC_HAS_LITERAL_ARG, 2, \
VM_OC_PUSH_TWO | VM_OC_GET_THIS_LITERAL) \
CBC_OPCODE (CBC_PUSH_NUMBER_0, CBC_NO_FLAG, 1, \
VM_OC_PUSH_NUMBER_0 | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NUMBER_POS_BYTE, CBC_HAS_BYTE_ARG, 1, \
VM_OC_PUSH_NUMBER_POS_BYTE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NUMBER_NEG_BYTE, CBC_HAS_BYTE_ARG, 1, \
VM_OC_PUSH_NUMBER_NEG_BYTE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP, CBC_NO_FLAG, -1, \
VM_OC_PROP_GET | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_PROP_GET | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_LITERAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
VM_OC_PROP_GET | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_THIS_LITERAL, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_PROP_GET | VM_OC_GET_THIS_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_IDENT_REFERENCE, CBC_HAS_LITERAL_ARG, 3, \
VM_OC_IDENT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_REFERENCE, CBC_NO_FLAG, 1, \
VM_OC_PROP_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG, 2, \
VM_OC_PROP_REFERENCE | VM_OC_GET_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 3, \
VM_OC_PROP_REFERENCE | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_THIS_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG, 3, \
VM_OC_PROP_REFERENCE | VM_OC_GET_THIS_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW0, CBC_NO_FLAG, 0, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW1, CBC_NO_FLAG, -1, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EVAL, CBC_NO_FLAG, 0, \
VM_OC_EVAL) \
CBC_OPCODE (CBC_DEFINE_VARS, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_INITIALIZE_VAR, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_INITIALIZE_VARS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_SET_BYTECODE_PTR, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_RETURN, CBC_NO_FLAG, -1, \
VM_OC_RET | VM_OC_GET_STACK) \
CBC_OPCODE (CBC_RETURN_WITH_BLOCK, CBC_NO_FLAG, 0, \
VM_OC_RET) \
CBC_OPCODE (CBC_RETURN_WITH_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_RET | VM_OC_GET_LITERAL) \
\
/* Unary opcodes. */ \
CBC_UNARY_OPERATION (CBC_PLUS, \
PLUS) \
CBC_UNARY_OPERATION (CBC_NEGATE, \
MINUS) \
CBC_UNARY_OPERATION (CBC_LOGICAL_NOT, \
NOT) \
CBC_UNARY_OPERATION (CBC_BIT_NOT, \
BIT_NOT) \
CBC_UNARY_OPERATION (CBC_VOID, \
VOID) \
CBC_OPCODE (CBC_TYPEOF, CBC_NO_FLAG, 0, \
VM_OC_TYPEOF | VM_OC_GET_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_TYPEOF_IDENT, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_TYPEOF_IDENT | VM_OC_PUT_STACK) \
\
/* Binary opcodes. */ \
CBC_BINARY_OPERATION (CBC_BIT_OR, \
BIT_OR) \
CBC_BINARY_OPERATION (CBC_BIT_XOR, \
BIT_XOR) \
CBC_BINARY_OPERATION (CBC_BIT_AND, \
BIT_AND) \
CBC_BINARY_OPERATION (CBC_EQUAL, \
EQUAL) \
CBC_BINARY_OPERATION (CBC_NOT_EQUAL, \
NOT_EQUAL) \
CBC_BINARY_OPERATION (CBC_STRICT_EQUAL, \
STRICT_EQUAL) \
CBC_BINARY_OPERATION (CBC_STRICT_NOT_EQUAL, \
STRICT_NOT_EQUAL) \
CBC_BINARY_OPERATION (CBC_LESS, \
LESS) \
CBC_BINARY_OPERATION (CBC_GREATER, \
GREATER) \
CBC_BINARY_OPERATION (CBC_LESS_EQUAL, \
LESS_EQUAL) \
CBC_BINARY_OPERATION (CBC_GREATER_EQUAL, \
GREATER_EQUAL) \
CBC_BINARY_OPERATION (CBC_IN, \
IN) \
CBC_BINARY_OPERATION (CBC_INSTANCEOF, \
INSTANCEOF) \
CBC_BINARY_OPERATION (CBC_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_BINARY_OPERATION (CBC_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_BINARY_OPERATION (CBC_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_BINARY_OPERATION (CBC_ADD, \
ADD) \
CBC_BINARY_OPERATION (CBC_SUBTRACT, \
SUB) \
CBC_BINARY_OPERATION (CBC_MULTIPLY, \
MUL) \
CBC_BINARY_OPERATION (CBC_DIVIDE, \
DIV) \
CBC_BINARY_OPERATION (CBC_MODULO, \
MOD) \
\
/* Unary lvalue opcodes. */ \
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
VM_OC_PROP_DELETE | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_DELETE_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_DELETE | VM_OC_PUT_STACK) \
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_INCR, \
PRE_INCR) \
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_DECR, \
PRE_DECR) \
CBC_UNARY_LVALUE_OPERATION (CBC_POST_INCR, \
POST_INCR) \
CBC_UNARY_LVALUE_OPERATION (CBC_POST_DECR, \
POST_DECR) \
\
/* Call opcodes. */ \
CBC_OPCODE (CBC_CALL, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL_PROP, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL_PROP_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL_PROP_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL0, CBC_NO_FLAG, -1, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL0_PUSH_RESULT, CBC_NO_FLAG, 0, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL0_BLOCK, CBC_NO_FLAG, -1, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL0_PROP, CBC_NO_FLAG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL0_PROP_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL0_PROP_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL1, CBC_NO_FLAG, -2, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL1_PUSH_RESULT, CBC_NO_FLAG, -1, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL1_BLOCK, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL1_PROP, CBC_NO_FLAG, -4, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL1_PROP_PUSH_RESULT, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL1_PROP_BLOCK, CBC_NO_FLAG, -4, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL2, CBC_NO_FLAG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL2_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL2_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL2_PROP, CBC_NO_FLAG, -4, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL2_PROP_PUSH_RESULT, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL2_PROP_BLOCK, CBC_NO_FLAG, -4, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
\
/* Binary assignment opcodes. */ \
CBC_OPCODE (CBC_ASSIGN, CBC_NO_FLAG, -3, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT_BLOCK, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT_BLOCK, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL, CBC_HAS_LITERAL_ARG, -2, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -2, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
\
/* Binary compound assignment opcodes. */ \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_ADD, \
ADD) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_SUBTRACT, \
SUB) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_MULTIPLY, \
MUL) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_DIVIDE, \
DIV) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_MODULO, \
MOD) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Last opcode (not a real opcode). */ \
CBC_OPCODE (CBC_END, CBC_NO_FLAG, 0, \
VM_OC_NONE)
/* All EXT branches are statement block end
* marks, so they are always forward branches. */
#define CBC_EXT_OPCODE_LIST \
/* Branch opcodes first. Some other opcodes are mixed. */ \
CBC_OPCODE (CBC_EXT_NOP, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_FORWARD_BRANCH (CBC_EXT_WITH_CREATE_CONTEXT, \
-1 + PARSER_WITH_CONTEXT_STACK_ALLOCATION, VM_OC_WITH) \
CBC_OPCODE (CBC_EXT_FOR_IN_GET_NEXT, CBC_NO_FLAG, 1, \
VM_OC_FOR_IN_GET_NEXT | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_EXT_FOR_IN_CREATE_CONTEXT, \
-1 + PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION, VM_OC_FOR_IN_CREATE_CONTEXT) \
CBC_OPCODE (CBC_EXT_SET_GETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_GETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_BACKWARD_BRANCH (CBC_EXT_BRANCH_IF_FOR_IN_HAS_NEXT, 0, \
VM_OC_FOR_IN_HAS_NEXT) \
CBC_OPCODE (CBC_EXT_SET_SETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_SETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_FORWARD_BRANCH (CBC_EXT_TRY_CREATE_CONTEXT, PARSER_TRY_CONTEXT_STACK_ALLOCATION, \
VM_OC_TRY) \
CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \
VM_OC_THROW_REFERENCE_ERROR) \
CBC_FORWARD_BRANCH (CBC_EXT_CATCH, 1, \
VM_OC_CATCH) \
CBC_OPCODE (CBC_EXT_PUSH_UNDEFINED_BASE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_UNDEFINED_BASE | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, 0, \
VM_OC_FINALLY) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_DEBUGGER, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
\
/* Binary compound assignment opcodes with pushing the result. */ \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_ADD, \
ADD) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_SUBTRACT, \
SUB) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_MULTIPLY, \
MUL) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_DIVIDE, \
DIV) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_MODULO, \
MOD) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Binary compound assignment opcodes with saving the result. */ \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_ADD, \
ADD) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_SUBTRACT, \
SUB) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_MULTIPLY, \
MUL) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_DIVIDE, \
DIV) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_MODULO, \
MOD) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Last opcode (not a real opcode). */ \
CBC_OPCODE (CBC_EXT_END, CBC_NO_FLAG, 0, \
VM_OC_NONE)
#define CBC_MAXIMUM_BYTE_VALUE 255
#define CBC_MAXIMUM_SMALL_VALUE 510
#define CBC_MAXIMUM_FULL_VALUE 32767
#define CBC_PUSH_NUMBER_BYTE_RANGE_END 256
#define CBC_HIGHEST_BIT_MASK 0x80
#define CBC_LOWER_SEVEN_BIT_MASK 0x7f
/**
* Literal indicies belong to one of the following groups:
*
* 0 <= index < argument_end : arguments
* argument_end <= index < register_end : registers
* register_end <= index < ident_end : identifiers
* ident_end <= index < const_literal_end : constant literals
* const_literal_end <= index < literal_end : template literals
*/
/**
* Compiled byte code arguments.
*/
typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
uint8_t stack_limit; /**< maximum number of values stored on the stack */
uint8_t argument_end; /**< number of arguments expected by the function */
uint8_t register_end; /**< end position of the register group */
uint8_t ident_end; /**< end position of the identifier group */
uint8_t const_literal_end; /**< end position of the const literal group */
uint8_t literal_end; /**< end position of the literal group */
} cbc_uint8_arguments_t;
/**
* Compiled byte code arguments.
*/
typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
uint16_t stack_limit; /**< maximum number of values stored on the stack */
uint16_t argument_end; /**< number of arguments expected by the function */
uint16_t register_end; /**< end position of the register group */
uint16_t ident_end; /**< end position of the identifier group */
uint16_t const_literal_end; /**< end position of the const literal group */
uint16_t literal_end; /**< end position of the literal group */
} cbc_uint16_arguments_t;
/* When CBC_CODE_FLAGS_FULL_LITERAL_ENCODING
* is not set the small encoding is used. */
#define CBC_CODE_FLAGS_FUNCTION 0x01
#define CBC_CODE_FLAGS_FULL_LITERAL_ENCODING 0x02
#define CBC_CODE_FLAGS_UINT16_ARGUMENTS 0x04
#define CBC_CODE_FLAGS_STRICT_MODE 0x08
#define CBC_CODE_FLAGS_ARGUMENTS_NEEDED 0x10
#define CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED 0x20
#define CBC_OPCODE(arg1, arg2, arg3, arg4) arg1,
/**
* Opcode list.
*/
typedef enum
{
CBC_OPCODE_LIST /**< list of opcodes */
} cbc_opcode_t;
/**
* Extended opcode list.
*/
typedef enum
{
CBC_EXT_OPCODE_LIST /**< list extended opcodes */
} cbc_ext_opcode_t;
#undef CBC_OPCODE
/**
* Opcode flags.
*/
extern const uint8_t cbc_flags[];
extern const uint8_t cbc_ext_flags[];
#ifdef PARSER_DUMP_BYTE_CODE
/**
* Opcode names for debugging.
*/
extern const char * const cbc_names[];
extern const char * const cbc_ext_names[];
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/
#endif /* !BYTE_CODE_H */

View file

@ -0,0 +1,136 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 "common.h"
#include "ecma-helpers.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/**
* Free literal.
*/
void
util_free_literal (lexer_literal_t *literal_p) /**< literal */
{
if (literal_p->type == LEXER_IDENT_LITERAL
|| literal_p->type == LEXER_STRING_LITERAL)
{
if (!(literal_p->status_flags & LEXER_FLAG_SOURCE_PTR))
{
jmem_heap_free_block ((void *) literal_p->u.char_p, literal_p->prop.length);
}
}
else if ((literal_p->type == LEXER_FUNCTION_LITERAL)
|| (literal_p->type == LEXER_REGEXP_LITERAL))
{
ecma_bytecode_deref (literal_p->u.bytecode_p);
}
} /* util_free_literal */
#ifdef PARSER_DUMP_BYTE_CODE
/**
* Debug utility to print a character sequence.
*/
static void
util_print_chars (const uint8_t *char_p, /**< character pointer */
size_t size) /**< size */
{
while (size > 0)
{
JERRY_DEBUG_MSG ("%c", *char_p++);
size--;
}
} /* util_print_chars */
/**
* Debug utility to print a number.
*/
static void
util_print_number (ecma_number_t num_p) /**< number to print */
{
lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf));
str_buf[str_size] = 0;
JERRY_DEBUG_MSG ("%s", str_buf);
} /* util_print_number */
/**
* Print literal.
*/
void
util_print_literal (lexer_literal_t *literal_p) /**< literal */
{
if (literal_p->type == LEXER_IDENT_LITERAL)
{
if (literal_p->status_flags & LEXER_FLAG_VAR)
{
JERRY_DEBUG_MSG ("var_ident(");
}
else
{
JERRY_DEBUG_MSG ("ident(");
}
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_FUNCTION_LITERAL)
{
JERRY_DEBUG_MSG ("function");
return;
}
else if (literal_p->type == LEXER_STRING_LITERAL)
{
JERRY_DEBUG_MSG ("string(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_NUMBER_LITERAL)
{
ecma_string_t *value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, literal_p->u.value);
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (value_p) == ECMA_STRING_LITERAL_NUMBER);
JERRY_DEBUG_MSG ("number(");
util_print_number (ecma_get_number_from_value (value_p->u.lit_number));
}
else if (literal_p->type == LEXER_REGEXP_LITERAL)
{
JERRY_DEBUG_MSG ("regexp");
return;
}
else
{
JERRY_DEBUG_MSG ("unknown");
return;
}
JERRY_DEBUG_MSG (")");
} /* util_print_literal */
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/

View file

@ -0,0 +1,137 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <setjmp.h>
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
#include "ecma-globals.h"
#include "ecma-regexp-object.h"
#include "jmem-heap.h"
/* Immediate management. */
/**
* Literal types.
*
* The LEXER_UNUSED_LITERAL type is internal and
* used for various purposes.
*/
typedef enum
{
LEXER_IDENT_LITERAL = 0, /**< identifier literal */
LEXER_STRING_LITERAL = 1, /**< string literal */
LEXER_NUMBER_LITERAL = 2, /**< number literal */
LEXER_FUNCTION_LITERAL = 3, /**< function literal */
LEXER_REGEXP_LITERAL = 4, /**< regexp literal */
LEXER_UNUSED_LITERAL = 5, /**< unused literal, can only be
used by the byte code generator. */
} lexer_literal_type_t;
/* Flags for status_flags. */
/* Local identifier (var, function arg). */
#define LEXER_FLAG_VAR 0x01
/* This local identifier cannot be stored in register. */
#define LEXER_FLAG_NO_REG_STORE 0x02
/* This local identifier is initialized with a value. */
#define LEXER_FLAG_INITIALIZED 0x04
/* This local identifier has a reference to the function itself. */
#define LEXER_FLAG_FUNCTION_NAME 0x08
/* This local identifier is a function argument. */
#define LEXER_FLAG_FUNCTION_ARGUMENT 0x10
/* No space is allocated for this character literal. */
#define LEXER_FLAG_SOURCE_PTR 0x20
/* Initialize this variable after the byte code is freed. */
#define LEXER_FLAG_LATE_INIT 0x40
/**
* Literal data.
*/
typedef struct
{
union
{
jmem_cpointer_t value; /**< literal value (not processed by the parser) */
const uint8_t *char_p; /**< character value */
ecma_compiled_code_t *bytecode_p; /**< compiled function or regexp pointer */
uint32_t source_data; /**< encoded source literal */
} u;
#ifdef PARSER_DUMP_BYTE_CODE
struct
#else /* !PARSER_DUMP_BYTE_CODE */
union
#endif /* PARSER_DUMP_BYTE_CODE */
{
uint16_t length; /**< length of ident / string literal */
uint16_t index; /**< real index during post processing */
} prop;
uint8_t type; /**< type of the literal */
uint8_t status_flags; /**< status flags */
} lexer_literal_t;
void util_free_literal (lexer_literal_t *);
#ifdef PARSER_DUMP_BYTE_CODE
void util_print_literal (lexer_literal_t *);
#endif /* PARSER_DUMP_BYTE_CODE */
/* TRY/CATCH block */
#define PARSER_TRY_CONTEXT(context_name) \
jmp_buf context_name
#define PARSER_THROW(context_name) \
longjmp (context_name, 1);
#define PARSER_TRY(context_name) \
{ \
if (!setjmp (context_name)) \
{ \
#define PARSER_CATCH \
} \
else \
{
#define PARSER_TRY_END \
} \
}
/**
* @}
* @}
* @}
*/
#endif /* !COMMON_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,269 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 JS_LEXER_H
#define JS_LEXER_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_lexer Lexer
* @{
*/
/**
* Lexer token types.
*/
typedef enum
{
LEXER_EOS, /**< end of source */
/* Primary expressions */
LEXER_LITERAL, /**< literal token */
LEXER_KEYW_THIS, /**< this */
LEXER_LIT_TRUE, /**< true (not a keyword!) */
LEXER_LIT_FALSE, /**< false (not a keyword!) */
LEXER_LIT_NULL, /**< null (not a keyword!) */
/* Unary operators
* IMPORTANT: update CBC_UNARY_OP_TOKEN_TO_OPCODE and
* CBC_UNARY_LVALUE_OP_TOKEN_TO_OPCODE after changes. */
#define LEXER_IS_UNARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_PLUS && (token_type) <= LEXER_DECREASE)
#define LEXER_IS_UNARY_LVALUE_OP_TOKEN(token_type) \
((token_type) >= LEXER_KEYW_DELETE && (token_type) <= LEXER_DECREASE)
LEXER_PLUS, /**< "+" */
LEXER_NEGATE, /**< "-" */
LEXER_LOGICAL_NOT, /**< "!" */
LEXER_BIT_NOT, /**< "~" */
LEXER_KEYW_VOID, /**< void */
LEXER_KEYW_TYPEOF, /**< typeof */
LEXER_KEYW_DELETE, /**< delete */
LEXER_INCREASE, /**< "++" */
LEXER_DECREASE, /**< "--" */
/* Binary operators
* IMPORTANT: update CBC_BINARY_OP_TOKEN_TO_OPCODE,
* CBC_BINARY_LVALUE_OP_TOKEN_TO_OPCODE and
* parser_binary_precedence_table after changes. */
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_MODULO)
#define LEXER_IS_BINARY_LVALUE_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_ASSIGN_BIT_XOR)
#define LEXER_FIRST_BINARY_OP LEXER_ASSIGN
LEXER_ASSIGN, /**< "=" (prec: 3) */
LEXER_ASSIGN_ADD, /**< "+=" (prec: 3) */
LEXER_ASSIGN_SUBTRACT, /**< "-=" (prec: 3) */
LEXER_ASSIGN_MULTIPLY, /**< "*=" (prec: 3) */
LEXER_ASSIGN_DIVIDE, /**< "/=" (prec: 3) */
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
LEXER_ASSIGN_UNS_RIGHT_SHIFT, /**< ">>>=" (prec: 3) */
LEXER_ASSIGN_BIT_AND, /**< "&=" (prec: 3) */
LEXER_ASSIGN_BIT_OR, /**< "|=" (prec: 3) */
LEXER_ASSIGN_BIT_XOR, /**< "^=" (prec: 3) */
LEXER_QUESTION_MARK, /**< "?" (prec: 4) */
LEXER_LOGICAL_OR, /**< "||" (prec: 5) */
LEXER_LOGICAL_AND, /**< "&&" (prec: 6) */
LEXER_BIT_OR, /**< "|" (prec: 7) */
LEXER_BIT_XOR, /**< "^" (prec: 8) */
LEXER_BIT_AND, /**< "&" (prec: 9) */
LEXER_EQUAL, /**< "==" (prec: 10) */
LEXER_NOT_EQUAL, /**< "!=" (prec: 10) */
LEXER_STRICT_EQUAL, /**< "===" (prec: 10) */
LEXER_STRICT_NOT_EQUAL, /**< "!==" (prec: 10) */
LEXER_LESS, /**< "<" (prec: 11) */
LEXER_GREATER, /**< ">" (prec: 11) */
LEXER_LESS_EQUAL, /**< "<=" (prec: 11) */
LEXER_GREATER_EQUAL, /**< ">=" (prec: 11) */
LEXER_KEYW_IN, /**< in (prec: 11) */
LEXER_KEYW_INSTANCEOF, /**< instanceof (prec: 11) */
LEXER_LEFT_SHIFT, /**< "<<" (prec: 12) */
LEXER_RIGHT_SHIFT, /**< ">>" (prec: 12) */
LEXER_UNS_RIGHT_SHIFT, /**< ">>>" (prec: 12) */
LEXER_ADD, /**< "+" (prec: 13) */
LEXER_SUBTRACT, /**< "-" (prec: 13) */
LEXER_MULTIPLY, /**< "*" (prec: 14) */
LEXER_DIVIDE, /**< "/" (prec: 14) */
LEXER_MODULO, /**< "%" (prec: 14) */
LEXER_LEFT_BRACE, /**< "{" */
LEXER_LEFT_PAREN, /**< "(" */
LEXER_LEFT_SQUARE, /**< "[" */
LEXER_RIGHT_BRACE, /**< "}" */
LEXER_RIGHT_PAREN, /**<_")" */
LEXER_RIGHT_SQUARE, /**< "]" */
LEXER_DOT, /**< "." */
LEXER_SEMICOLON, /**< ";" */
LEXER_COLON, /**< ":" */
LEXER_COMMA, /**< "," */
LEXER_KEYW_BREAK, /**< break */
LEXER_KEYW_DO, /**< do */
LEXER_KEYW_CASE, /**< case */
LEXER_KEYW_ELSE, /**< else */
LEXER_KEYW_NEW, /**< new */
LEXER_KEYW_VAR, /**< var */
LEXER_KEYW_CATCH, /**< catch */
LEXER_KEYW_FINALLY, /**< finally */
LEXER_KEYW_RETURN, /**< return */
LEXER_KEYW_CONTINUE, /**< continue */
LEXER_KEYW_FOR, /**< for */
LEXER_KEYW_SWITCH, /**< switch */
LEXER_KEYW_WHILE, /**< while */
LEXER_KEYW_DEBUGGER, /**< debugger */
LEXER_KEYW_FUNCTION, /**< function */
LEXER_KEYW_WITH, /**< with */
LEXER_KEYW_DEFAULT, /**< default */
LEXER_KEYW_IF, /**< if */
LEXER_KEYW_THROW, /**< throw */
LEXER_KEYW_TRY, /**< try */
/* These are virtual tokens. */
LEXER_EXPRESSION_START, /**< expression start */
LEXER_PROPERTY_GETTER, /**< property getter function */
LEXER_PROPERTY_SETTER, /**< property setter function */
LEXER_COMMA_SEP_LIST, /**< comma separated bracketed expression list */
LEXER_SCAN_SWITCH, /**< special value for switch pre-scan */
/* Future reserved words: these keywords
* must form a group after all other keywords. */
#define LEXER_FIRST_FUTURE_RESERVED_WORD LEXER_KEYW_CLASS
LEXER_KEYW_CLASS, /**< class */
LEXER_KEYW_ENUM, /**< enum */
LEXER_KEYW_EXTENDS, /**< extends */
LEXER_KEYW_SUPER, /**< super */
LEXER_KEYW_CONST, /**< const */
LEXER_KEYW_EXPORT, /**< export */
LEXER_KEYW_IMPORT, /**< import */
/* Future strict reserved words: these keywords
* must form a group after future reserved words. */
#define LEXER_FIRST_FUTURE_STRICT_RESERVED_WORD LEXER_KEYW_IMPLEMENTS
LEXER_KEYW_IMPLEMENTS, /**< implements */
LEXER_KEYW_LET, /**< let */
LEXER_KEYW_PRIVATE, /**< private */
LEXER_KEYW_PUBLIC, /**< public */
LEXER_KEYW_YIELD, /**< yield */
LEXER_KEYW_INTERFACE, /**< interface */
LEXER_KEYW_PACKAGE, /**< package */
LEXER_KEYW_PROTECTED, /**< protected */
LEXER_KEYW_STATIC, /**< static */
} lexer_token_type_t;
#define LEXER_NEWLINE_LS_PS_BYTE_1 0xe2
#define LEXER_NEWLINE_LS_PS_BYTE_23(source) \
((source)[1] == LIT_UTF8_2_BYTE_CODE_POINT_MIN && ((source)[2] | 0x1) == 0xa9)
#define LEXER_UTF8_4BYTE_START 0xf0
#define LEXER_IS_LEFT_BRACKET(type) \
((type) == LEXER_LEFT_BRACE || (type) == LEXER_LEFT_PAREN || (type) == LEXER_LEFT_SQUARE)
#define LEXER_IS_RIGHT_BRACKET(type) \
((type) == LEXER_RIGHT_BRACE || (type) == LEXER_RIGHT_PAREN || (type) == LEXER_RIGHT_SQUARE)
#define LEXER_UNARY_OP_TOKEN_TO_OPCODE(token_type) \
((((token_type) - LEXER_PLUS) * 2) + CBC_PLUS)
#define LEXER_UNARY_LVALUE_OP_TOKEN_TO_OPCODE(token_type) \
((((token_type) - LEXER_INCREASE) * 6) + CBC_PRE_INCR)
#define LEXER_BINARY_OP_TOKEN_TO_OPCODE(token_type) \
((cbc_opcode_t) ((((token_type) - LEXER_BIT_OR) * 3) + CBC_BIT_OR))
#define LEXER_BINARY_LVALUE_OP_TOKEN_TO_OPCODE(token_type) \
((cbc_opcode_t) ((((token_type) - LEXER_ASSIGN_ADD) * 2) + CBC_ASSIGN_ADD))
/**
* Lexer literal object types.
*/
typedef enum
{
LEXER_LITERAL_OBJECT_ANY, /**< unspecified object type */
LEXER_LITERAL_OBJECT_EVAL, /**< reference is equal to eval */
LEXER_LITERAL_OBJECT_ARGUMENTS, /**< reference is equal to arguments */
} lexer_literal_object_type_t;
/**
* Lexer number types.
*/
typedef enum
{
LEXER_NUMBER_DECIMAL, /**< decimal number */
LEXER_NUMBER_HEXADECIMAL, /**< hexadecimal number */
LEXER_NUMBER_OCTAL, /**< octal number */
} lexer_number_type_t;
/**
* Lexer character (string / identifier) literal data.
*/
typedef struct
{
const uint8_t *char_p; /**< start of identifier or string token */
uint16_t length; /**< length or index of a literal */
uint8_t type; /**< type of the current literal */
uint8_t has_escape; /**< has escape sequences */
} lexer_lit_location_t;
/**
* Range of input string which processing is postponed.
*/
typedef struct
{
const uint8_t *source_p; /**< next source byte */
const uint8_t *source_end_p; /**< last source byte */
parser_line_counter_t line; /**< token start line */
parser_line_counter_t column; /**< token start column */
} lexer_range_t;
/**
* Lexer token.
*/
typedef struct
{
uint8_t type; /**< token type */
uint8_t literal_is_reserved; /**< future reserved keyword
* (when char_literal.type is LEXER_IDENT_LITERAL) */
uint8_t extra_value; /**< helper value for different purposes */
uint8_t was_newline; /**< newline occured before this token */
parser_line_counter_t line; /**< token start line */
parser_line_counter_t column; /**< token start column */
lexer_lit_location_t lit_location; /**< extra data for character literals */
} lexer_token_t;
/**
* Literal data set by lexer_construct_literal_object.
*/
typedef struct
{
lexer_literal_t *literal_p; /**< pointer to the literal object */
uint16_t index; /**< literal index */
uint8_t type; /**< literal object type */
} lexer_lit_object_t;
/**
* @}
* @}
* @}
*/
#endif /* !JS_LEXER_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,436 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 JS_PARSER_INTERNAL_H
#define JS_PARSER_INTERNAL_H
#include "common.h"
#include "byte-code.h"
#include "js-parser.h"
#include "js-parser-limits.h"
#include "js-lexer.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_internals Internals
* @{
*/
/* General parser flags. */
#define PARSER_IS_STRICT 0x00001u
#define PARSER_IS_FUNCTION 0x00002u
#define PARSER_IS_CLOSURE 0x00004u
#define PARSER_IS_PROPERTY_GETTER 0x00008u
#define PARSER_IS_PROPERTY_SETTER 0x00010u
#define PARSER_IS_FUNC_EXPRESSION 0x00020u
#define PARSER_HAS_NON_STRICT_ARG 0x00040u
#define PARSER_INSIDE_WITH 0x00080u
#define PARSER_RESOLVE_THIS_FOR_CALLS 0x00100u
#define PARSER_NAMED_FUNCTION_EXP 0x00200u
#define PARSER_HAS_INITIALIZED_VARS 0x00400u
#define PARSER_NO_END_LABEL 0x00800u
#define PARSER_NO_REG_STORE 0x01000u
#define PARSER_ARGUMENTS_NEEDED 0x02000u
#define PARSER_ARGUMENTS_NOT_NEEDED 0x04000u
#define PARSER_LEXICAL_ENV_NEEDED 0x08000u
#define PARSER_HAS_LATE_LIT_INIT 0x10000u
/* Expression parsing flags. */
#define PARSE_EXPR 0x00
#define PARSE_EXPR_STATEMENT 0x01
#define PARSE_EXPR_BLOCK 0x02
#define PARSE_EXPR_NO_COMMA 0x04
#define PARSE_EXPR_HAS_LITERAL 0x08
/* The maximum of PARSER_CBC_STREAM_PAGE_SIZE is 127. */
#define PARSER_CBC_STREAM_PAGE_SIZE \
((uint32_t) (64 - sizeof (void *)))
#define PARSER_STACK_PAGE_SIZE \
((uint32_t) (((sizeof (void *) > 4) ? 128 : 64) - sizeof (void *)))
/* Avoid compiler warnings for += operations. */
#define PARSER_PLUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) + (value))
#define PARSER_MINUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) - (value))
#define PARSER_PLUS_EQUAL_LC(base, value) (base) = (parser_line_counter_t) ((base) + (value))
/**
* Argument for a compact-byte code.
*/
typedef struct
{
uint16_t literal_index; /**< literal index argument */
uint16_t value; /**< other argument (second literal or byte). */
uint16_t third_literal_index; /**< literal index argument */
uint8_t literal_type; /**< last literal type */
uint8_t literal_object_type; /**< last literal object type */
} cbc_argument_t;
/* Useful parser macros. */
#define PARSER_CBC_UNAVAILABLE CBC_EXT_OPCODE
#define PARSER_TO_EXT_OPCODE(opcode) ((uint16_t) ((opcode) + 256))
#define PARSER_GET_EXT_OPCODE(opcode) ((opcode) - 256)
#define PARSER_IS_BASIC_OPCODE(opcode) ((opcode) < 256)
#define PARSER_IS_PUSH_LITERAL(opcode) \
((opcode) == CBC_PUSH_LITERAL \
|| (opcode) == CBC_PUSH_TWO_LITERALS \
|| (opcode) == CBC_PUSH_THREE_LITERALS)
#define PARSER_GET_LITERAL(literal_index) \
((lexer_literal_t *) parser_list_get (&context_p->literal_pool, (literal_index)))
#define PARSER_TO_BINARY_OPERATION_WITH_RESULT(opcode) \
(PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_PUSH_RESULT)
#define PARSER_TO_BINARY_OPERATION_WITH_BLOCK(opcode) \
((uint16_t) (PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_BLOCK))
#define PARSER_GET_FLAGS(op) \
(PARSER_IS_BASIC_OPCODE (op) ? cbc_flags[(op)] : cbc_ext_flags[PARSER_GET_EXT_OPCODE (op)])
#define PARSER_OPCODE_IS_RETURN(op) \
((op) == CBC_RETURN || (op) == CBC_RETURN_WITH_BLOCK || (op) == CBC_RETURN_WITH_LITERAL)
#define PARSER_ARGS_EQ(op, types) \
((PARSER_GET_FLAGS (op) & CBC_ARG_TYPES) == (types))
/**
* All data allocated by the parser is
* stored in parser_data_pages in the memory.
*/
typedef struct parser_mem_page_t
{
struct parser_mem_page_t *next_p; /**< next page */
uint8_t bytes[1]; /**< memory bytes */
} parser_mem_page_t;
/**
* Structure for managing parser memory.
*/
typedef struct
{
parser_mem_page_t *first_p; /**< first allocated page */
parser_mem_page_t *last_p; /**< last allocated page */
uint32_t last_position; /**< position of the last allocated byte */
} parser_mem_data_t;
/**
* Parser memory list.
*/
typedef struct
{
parser_mem_data_t data; /**< storage space */
uint32_t page_size; /**< size of each page */
uint32_t item_size; /**< size of each item */
uint32_t item_count; /**< number of items on each page */
} parser_list_t;
/**
* Iterator for parser memory list.
*/
typedef struct
{
parser_list_t *list_p; /**< parser list */
parser_mem_page_t *current_p; /**< currently processed page */
size_t current_position; /**< current position on the page */
} parser_list_iterator_t;
/**
* Parser memory stack.
*/
typedef struct
{
parser_mem_data_t data; /**< storage space */
parser_mem_page_t *free_page_p; /**< space for fast allocation */
} parser_stack_t;
/**
* Iterator for parser memory stack.
*/
typedef struct
{
parser_mem_page_t *current_p; /**< currently processed page */
size_t current_position; /**< current position on the page */
} parser_stack_iterator_t;
/**
* Branch type.
*/
typedef struct
{
parser_mem_page_t *page_p; /**< branch location page */
uint32_t offset; /**< branch location offset */
} parser_branch_t;
/**
* Branch chain type.
*/
typedef struct parser_branch_node_t
{
struct parser_branch_node_t *next_p; /**< next linked list node */
parser_branch_t branch; /**< branch */
} parser_branch_node_t;
/**
* Those members of a context which needs
* to be saved when a sub-function is parsed.
*/
typedef struct parser_saved_context_t
{
/* Parser members. */
uint32_t status_flags; /**< parsing options */
uint16_t stack_depth; /**< current stack depth */
uint16_t stack_limit; /**< maximum stack depth */
struct parser_saved_context_t *prev_context_p; /**< last saved context */
parser_stack_iterator_t last_statement; /**< last statement position */
/* Literal types */
uint16_t argument_count; /**< number of function arguments */
uint16_t register_count; /**< number of registers */
uint16_t literal_count; /**< number of literals */
/* Memory storage members. */
parser_mem_data_t byte_code; /**< byte code buffer */
uint32_t byte_code_size; /**< byte code size for branches */
parser_mem_data_t literal_pool_data; /**< literal list */
#ifndef JERRY_NDEBUG
uint16_t context_stack_depth; /**< current context stack depth */
#endif /* !JERRY_NDEBUG */
} parser_saved_context_t;
/**
* Shared parser context.
*/
typedef struct
{
PARSER_TRY_CONTEXT (try_buffer); /**< try_buffer */
parser_error_t error; /**< error code */
void *allocated_buffer_p; /**< dinamically allocated buffer
* which needs to be freed on error */
uint32_t allocated_buffer_size; /**< size of the dinamically allocated buffer */
/* Parser members. */
uint32_t status_flags; /**< status flags */
uint16_t stack_depth; /**< current stack depth */
uint16_t stack_limit; /**< maximum stack depth */
parser_saved_context_t *last_context_p; /**< last saved context */
parser_stack_iterator_t last_statement; /**< last statement position */
/* Lexer members. */
lexer_token_t token; /**< current token */
lexer_lit_object_t lit_object; /**< current literal object */
const uint8_t *source_p; /**< next source byte */
const uint8_t *source_end_p; /**< last source byte */
parser_line_counter_t line; /**< current line */
parser_line_counter_t column; /**< current column */
/* Compact byte code members. */
cbc_argument_t last_cbc; /**< argument of the last cbc */
uint16_t last_cbc_opcode; /**< opcode of the last cbc */
/* Literal types */
uint16_t argument_count; /**< number of function arguments */
uint16_t register_count; /**< number of registers */
uint16_t literal_count; /**< number of literals */
/* Memory storage members. */
parser_mem_data_t byte_code; /**< byte code buffer */
uint32_t byte_code_size; /**< current byte code size for branches */
parser_list_t literal_pool; /**< literal list */
parser_mem_data_t stack; /**< storage space */
parser_mem_page_t *free_page_p; /**< space for fast allocation */
uint8_t stack_top_uint8; /**< top byte stored on the stack */
#ifndef JERRY_NDEBUG
/* Variables for debugging / logging. */
uint16_t context_stack_depth; /**< current context stack depth */
#endif /* !JERRY_NDEBUG */
#ifdef PARSER_DUMP_BYTE_CODE
int is_show_opcodes; /**< show opcodes */
uint32_t total_byte_code_size; /**< total byte code size */
#endif /* PARSER_DUMP_BYTE_CODE */
} parser_context_t;
/**
* @}
* @}
* @}
*
* \addtogroup mem Memory allocation
* @{
*
* \addtogroup mem_parser Parser memory manager
* @{
*/
/* Memory management.
* Note: throws an error if unsuccessful. */
void *parser_malloc (parser_context_t *, size_t);
void parser_free (void *, size_t);
void *parser_malloc_local (parser_context_t *, size_t);
void parser_free_local (void *, size_t);
/* Parser byte stream. */
void parser_cbc_stream_init (parser_mem_data_t *);
void parser_cbc_stream_free (parser_mem_data_t *);
void parser_cbc_stream_alloc_page (parser_context_t *, parser_mem_data_t *);
/* Parser list. Ensures pointer alignment. */
void parser_list_init (parser_list_t *, uint32_t, uint32_t);
void parser_list_free (parser_list_t *);
void parser_list_reset (parser_list_t *);
void *parser_list_append (parser_context_t *, parser_list_t *);
void *parser_list_get (parser_list_t *, size_t);
void parser_list_iterator_init (parser_list_t *, parser_list_iterator_t *);
void *parser_list_iterator_next (parser_list_iterator_t *);
/* Parser stack. Optimized for pushing bytes.
* Pop functions never throws error. */
void parser_stack_init (parser_context_t *);
void parser_stack_free (parser_context_t *);
void parser_stack_push_uint8 (parser_context_t *, uint8_t);
void parser_stack_pop_uint8 (parser_context_t *);
void parser_stack_push_uint16 (parser_context_t *, uint16_t);
uint16_t parser_stack_pop_uint16 (parser_context_t *);
void parser_stack_push (parser_context_t *, const void *, uint32_t);
void parser_stack_pop (parser_context_t *, void *, uint32_t);
void parser_stack_iterator_skip (parser_stack_iterator_t *, size_t);
void parser_stack_iterator_read (parser_stack_iterator_t *, void *, size_t);
void parser_stack_iterator_write (parser_stack_iterator_t *, const void *, size_t);
/**
* @}
* @}
*
* \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/* Compact byte code emitting functions. */
void parser_flush_cbc (parser_context_t *);
void parser_emit_cbc (parser_context_t *, uint16_t);
void parser_emit_cbc_literal (parser_context_t *, uint16_t, uint16_t);
void parser_emit_cbc_literal_from_token (parser_context_t *, uint16_t);
void parser_emit_cbc_call (parser_context_t *, uint16_t, size_t);
void parser_emit_cbc_push_number (parser_context_t *, bool);
void parser_emit_cbc_forward_branch (parser_context_t *, uint16_t, parser_branch_t *);
parser_branch_node_t *parser_emit_cbc_forward_branch_item (parser_context_t *, uint16_t, parser_branch_node_t *);
void parser_emit_cbc_backward_branch (parser_context_t *, uint16_t, uint32_t);
void parser_set_branch_to_current_position (parser_context_t *, parser_branch_t *);
void parser_set_breaks_to_current_position (parser_context_t *, parser_branch_node_t *);
void parser_set_continues_to_current_position (parser_context_t *, parser_branch_node_t *);
/* Convenience macros. */
#define parser_emit_cbc_ext(context_p, opcode) \
parser_emit_cbc ((context_p), PARSER_TO_EXT_OPCODE (opcode))
#define parser_emit_cbc_ext_literal(context_p, opcode, literal_index) \
parser_emit_cbc_literal ((context_p), PARSER_TO_EXT_OPCODE (opcode), (literal_index))
#define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \
parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments))
#define parser_emit_cbc_ext_forward_branch(context_p, opcode, branch_p) \
parser_emit_cbc_forward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (branch_p))
#define parser_emit_cbc_ext_backward_branch(context_p, opcode, offset) \
parser_emit_cbc_backward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (offset))
/**
* @}
*
* \addtogroup jsparser_lexer Lexer
* @{
*/
/* Lexer functions */
void lexer_next_token (parser_context_t *);
void lexer_expect_identifier (parser_context_t *, uint8_t);
void lexer_scan_identifier (parser_context_t *, bool);
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
void lexer_expect_object_literal_id (parser_context_t *, bool);
void lexer_construct_literal_object (parser_context_t *, lexer_lit_location_t *, uint8_t);
bool lexer_construct_number_object (parser_context_t *, bool, bool);
void lexer_construct_function_object (parser_context_t *, uint32_t);
void lexer_construct_regexp_object (parser_context_t *, bool);
bool lexer_compare_identifier_to_current (parser_context_t *, const lexer_lit_location_t *);
/**
* @}
*
* \addtogroup jsparser_expr Expression parser
* @{
*/
/* Parser functions. */
void parser_parse_expression (parser_context_t *, int);
/**
* @}
*
* \addtogroup jsparser_scanner Scanner
* @{
*/
void parser_scan_until (parser_context_t *, lexer_range_t *, lexer_token_type_t);
/**
* @}
*
* \addtogroup jsparser_stmt Statement parser
* @{
*/
void parser_parse_statements (parser_context_t *);
void parser_free_jumps (parser_stack_iterator_t);
/**
* @}
*
* \addtogroup jsparser_parser Parser
* @{
*/
ecma_compiled_code_t *parser_parse_function (parser_context_t *, uint32_t);
/* Error management. */
void parser_raise_error (parser_context_t *, parser_error_t);
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_INTERNAL_H */

View file

@ -0,0 +1,99 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 JS_PARSER_LIMITS_H
#define JS_PARSER_LIMITS_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_internals Internals
* @{
*/
/* Maximum identifier length accepted by the parser.
* Limit: LEXER_MAX_STRING_LENGTH. */
#ifndef PARSER_MAXIMUM_IDENT_LENGTH
#define PARSER_MAXIMUM_IDENT_LENGTH 255
#endif /* !PARSER_MAXIMUM_IDENT_LENGTH */
/* Maximum string length.
* Limit: 65535. */
#ifndef PARSER_MAXIMUM_STRING_LENGTH
#define PARSER_MAXIMUM_STRING_LENGTH 65535
#endif /* !PARSER_MAXIMUM_STRING_LENGTH */
/* Maximum number of literals.
* Limit: 32767. Recommended: 510, 32767 */
#ifndef PARSER_MAXIMUM_NUMBER_OF_LITERALS
#define PARSER_MAXIMUM_NUMBER_OF_LITERALS 32767
#endif /* !PARSER_MAXIMUM_NUMBER_OF_LITERALS */
/* Maximum number of registers.
* Limit: PARSER_MAXIMUM_NUMBER_OF_LITERALS */
#ifndef PARSER_MAXIMUM_NUMBER_OF_REGISTERS
#define PARSER_MAXIMUM_NUMBER_OF_REGISTERS 256
#endif /* !PARSER_MAXIMUM_NUMBER_OF_REGISTERS */
/* Maximum code size.
* Limit: 16777215. Recommended: 65535, 16777215. */
#ifndef PARSER_MAXIMUM_CODE_SIZE
#define PARSER_MAXIMUM_CODE_SIZE (65535 << (JMEM_ALIGNMENT_LOG))
#endif /* !PARSER_MAXIMUM_CODE_SIZE */
/* Maximum number of values pushed onto the stack by a function.
* Limit: 65500. Recommended: 1024. */
#ifndef PARSER_MAXIMUM_STACK_LIMIT
#define PARSER_MAXIMUM_STACK_LIMIT 1024
#endif /* !PARSER_MAXIMUM_STACK_LIMIT */
/* Checks. */
#if (PARSER_MAXIMUM_STRING_LENGTH < 1) || (PARSER_MAXIMUM_STRING_LENGTH > 65535)
#error "Maximum string length is not within range."
#endif /* (PARSER_MAXIMUM_STRING_LENGTH < 1) || (PARSER_MAXIMUM_STRING_LENGTH > 65535) */
#if (PARSER_MAXIMUM_IDENT_LENGTH < 1) || (PARSER_MAXIMUM_IDENT_LENGTH > PARSER_MAXIMUM_STRING_LENGTH)
#error "Maximum identifier length is not within range."
#endif /* (PARSER_MAXIMUM_IDENT_LENGTH < 1) || (PARSER_MAXIMUM_IDENT_LENGTH > PARSER_MAXIMUM_STRING_LENGTH) */
#if (PARSER_MAXIMUM_NUMBER_OF_LITERALS < 1) || (PARSER_MAXIMUM_NUMBER_OF_LITERALS > 32767)
#error "Maximum number of literals is not within range."
#endif /* (PARSER_MAXIMUM_NUMBER_OF_LITERALS < 1) || (PARSER_MAXIMUM_NUMBER_OF_LITERALS > 32767) */
#if (PARSER_MAXIMUM_NUMBER_OF_REGISTERS > PARSER_MAXIMUM_NUMBER_OF_LITERALS)
#error "Maximum number of registers is not within range."
#endif /* (PARSER_MAXIMUM_NUMBER_OF_REGISTERS > PARSER_MAXIMUM_NUMBER_OF_LITERALS) */
#if (PARSER_MAXIMUM_CODE_SIZE < 4096) || (PARSER_MAXIMUM_CODE_SIZE > 16777215)
#error "Maximum code size is not within range."
#endif /* (PARSER_MAXIMUM_CODE_SIZE < 4096) || (PARSER_MAXIMUM_CODE_SIZE > 16777215) */
#if (PARSER_MAXIMUM_STACK_LIMIT < 16) || (PARSER_MAXIMUM_STACK_LIMIT > 65500)
#error "Maximum function stack usage is not within range."
#endif /* (PARSER_MAXIMUM_STACK_LIMIT < 16) || (PARSER_MAXIMUM_STACK_LIMIT > 65500) */
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_LIMITS_H */

View file

@ -0,0 +1,678 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 "js-parser-internal.h"
/** \addtogroup mem Memory allocation
* @{
*
* \addtogroup mem_parser Parser memory manager
* @{
*/
/**********************************************************************/
/* Memory allocation */
/**********************************************************************/
/**
* Allocate memory.
*
* @return allocated memory.
*/
void *
parser_malloc (parser_context_t *context_p, /**< context */
size_t size) /**< size of the memory block */
{
void *result;
JERRY_ASSERT (size > 0);
result = jmem_heap_alloc_block_null_on_error (size);
if (result == NULL)
{
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
return result;
} /* parser_malloc */
/**
* Free memory allocated by parser_malloc.
*/
void parser_free (void *ptr, /**< pointer to free */
size_t size) /**< size of the memory block */
{
jmem_heap_free_block (ptr, size);
} /* parser_free */
/**
* Allocate local memory for short term use.
*
* @return allocated memory.
*/
void *
parser_malloc_local (parser_context_t *context_p, /**< context */
size_t size) /**< size of the memory */
{
void *result;
JERRY_ASSERT (size > 0);
result = jmem_heap_alloc_block (size);
if (result == 0)
{
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
return result;
} /* parser_malloc_local */
/**
* Free memory allocated by parser_malloc_local.
*/
void parser_free_local (void *ptr, /**< pointer to free */
size_t size) /**< size of the memory */
{
jmem_heap_free_block (ptr, size);
} /* parser_free_local */
/**********************************************************************/
/* Parser data management functions */
/**********************************************************************/
/**
* Initialize parse data.
*/
static void
parser_data_init (parser_mem_data_t *data_p, /**< memory manager */
uint32_t page_size) /**< size of each page */
{
data_p->first_p = NULL;
data_p->last_p = NULL;
data_p->last_position = page_size;
} /* parser_data_init */
/**
* Free parse data.
*/
static void
parser_data_free (parser_mem_data_t *data_p, /**< memory manager */
uint32_t page_size) /**< size of each page */
{
parser_mem_page_t *page_p = data_p->first_p;
while (page_p != NULL)
{
parser_mem_page_t *next_p = page_p->next_p;
parser_free (page_p, page_size);
page_p = next_p;
}
} /* parser_data_free */
/**********************************************************************/
/* Parser byte stream management functions */
/**********************************************************************/
/**
* Initialize byte stream.
*/
void
parser_cbc_stream_init (parser_mem_data_t *data_p) /**< memory manager */
{
parser_data_init (data_p, PARSER_CBC_STREAM_PAGE_SIZE);
} /* parser_cbc_stream_init */
/**
* Free byte stream.
*/
void
parser_cbc_stream_free (parser_mem_data_t *data_p) /**< memory manager */
{
parser_data_free (data_p,
sizeof (parser_mem_page_t *) + PARSER_CBC_STREAM_PAGE_SIZE);
} /* parser_cbc_stream_free */
/**
* Appends a byte at the end of the byte stream.
*/
void
parser_cbc_stream_alloc_page (parser_context_t *context_p, /**< context */
parser_mem_data_t *data_p) /**< memory manager */
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_CBC_STREAM_PAGE_SIZE;
parser_mem_page_t *page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
page_p->next_p = NULL;
data_p->last_position = 0;
if (data_p->last_p != NULL)
{
data_p->last_p->next_p = page_p;
}
else
{
data_p->first_p = page_p;
}
data_p->last_p = page_p;
} /* parser_cbc_stream_alloc_page */
/**********************************************************************/
/* Parser list management functions */
/**********************************************************************/
/**
* Initialize parser list.
*/
void
parser_list_init (parser_list_t *list_p, /**< parser list */
uint32_t item_size, /**< size for each page */
uint32_t item_count) /**< number of items on each page */
{
/* Align to pointer size. */
item_size = (uint32_t) (((item_size) + sizeof (void *) - 1) & ~(sizeof (void *) - 1));
parser_data_init (&list_p->data, item_size * item_count);
list_p->page_size = item_size * item_count;
list_p->item_size = item_size;
list_p->item_count = item_count;
} /* parser_list_init */
/**
* Free parser list.
*/
void
parser_list_free (parser_list_t *list_p) /**< parser list */
{
parser_data_free (&list_p->data,
(uint32_t) (sizeof (parser_mem_page_t *) + list_p->page_size));
} /* parser_list_free */
/**
* Reset parser list.
*/
void
parser_list_reset (parser_list_t *list_p) /**< parser list */
{
parser_data_init (&list_p->data, list_p->page_size);
} /* parser_list_reset */
/**
* Allocate space for the next item.
*
* @return pointer to the appended item.
*/
void *
parser_list_append (parser_context_t *context_p, /**< context */
parser_list_t *list_p) /**< parser list */
{
parser_mem_page_t *page_p = list_p->data.last_p;
void *result;
if (list_p->data.last_position + list_p->item_size > list_p->page_size)
{
size_t size = sizeof (parser_mem_page_t *) + list_p->page_size;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
page_p->next_p = NULL;
list_p->data.last_position = 0;
if (list_p->data.last_p != NULL)
{
list_p->data.last_p->next_p = page_p;
}
else
{
list_p->data.first_p = page_p;
}
list_p->data.last_p = page_p;
}
result = page_p->bytes + list_p->data.last_position;
list_p->data.last_position += list_p->item_size;
return result;
} /* parser_list_append */
/**
* Return the nth item of the list.
*
* @return pointer to the item.
*/
void *
parser_list_get (parser_list_t *list_p, /**< parser list */
size_t index) /**< item index */
{
size_t item_count = list_p->item_count;
parser_mem_page_t *page_p = list_p->data.first_p;
while (index >= item_count)
{
JERRY_ASSERT (page_p != NULL);
page_p = page_p->next_p;
index -= item_count;
}
JERRY_ASSERT (page_p != NULL);
JERRY_ASSERT (page_p != list_p->data.last_p
|| (index * list_p->item_size < list_p->data.last_position));
return page_p->bytes + (index * list_p->item_size);
} /* parser_list_get */
/**
* Initialize a parser list iterator.
*/
void
parser_list_iterator_init (parser_list_t *list_p, /**< parser list */
parser_list_iterator_t *iterator_p) /**< iterator */
{
iterator_p->list_p = list_p;
iterator_p->current_p = list_p->data.first_p;
iterator_p->current_position = 0;
} /* parser_list_iterator_init */
/**
* Next iterator step.
*
* @return the address of the current item, or NULL at the end.
*/
void *
parser_list_iterator_next (parser_list_iterator_t *iterator_p) /**< iterator */
{
void *result;
if (iterator_p->current_p == NULL)
{
return NULL;
}
result = iterator_p->current_p->bytes + iterator_p->current_position;
iterator_p->current_position += iterator_p->list_p->item_size;
if (iterator_p->current_p->next_p == NULL)
{
if (iterator_p->current_position >= iterator_p->list_p->data.last_position)
{
iterator_p->current_p = NULL;
iterator_p->current_position = 0;
}
}
else if (iterator_p->current_position >= iterator_p->list_p->page_size)
{
iterator_p->current_p = iterator_p->current_p->next_p;
iterator_p->current_position = 0;
}
return result;
} /* parser_list_iterator_next */
/**********************************************************************/
/* Parser stack management functions */
/**********************************************************************/
/* Stack is a reversed storage. */
/**
* Initialize parser stack.
*/
void
parser_stack_init (parser_context_t *context_p) /**< context */
{
parser_data_init (&context_p->stack, PARSER_STACK_PAGE_SIZE);
context_p->free_page_p = NULL;
} /* parser_stack_init */
/**
* Free parser stack.
*/
void
parser_stack_free (parser_context_t *context_p) /**< context */
{
parser_data_free (&context_p->stack,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
if (context_p->free_page_p != NULL)
{
parser_free (context_p->free_page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
} /* parser_stack_free */
/**
* Pushes an uint8_t value onto the stack.
*/
void
parser_stack_push_uint8 (parser_context_t *context_p, /**< context */
uint8_t uint8_value) /**< value pushed onto the stack */
{
parser_mem_page_t *page_p = context_p->stack.first_p;
/* This assert might trigger false positive valgrind errors, when
* parser_stack_push() pushes not fully initialized structures.
* More precisely when the last byte of the structure is uninitialized. */
JERRY_ASSERT (page_p == NULL
|| context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
if (context_p->stack.last_position >= PARSER_STACK_PAGE_SIZE)
{
if (context_p->free_page_p != NULL)
{
page_p = context_p->free_page_p;
context_p->free_page_p = NULL;
}
else
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
}
page_p->next_p = context_p->stack.first_p;
context_p->stack.last_position = 0;
context_p->stack.first_p = page_p;
}
page_p->bytes[context_p->stack.last_position++] = uint8_value;
context_p->stack_top_uint8 = uint8_value;
} /* parser_stack_push_uint8 */
/**
* Pops the last uint8_t value from the stack.
*/
void
parser_stack_pop_uint8 (parser_context_t *context_p) /**< context */
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
context_p->stack.last_position--;
if (context_p->stack.last_position == 0)
{
context_p->stack.first_p = page_p->next_p;
context_p->stack.last_position = PARSER_STACK_PAGE_SIZE;
if (context_p->free_page_p == NULL)
{
context_p->free_page_p = page_p;
}
else
{
parser_free (page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL);
}
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 1];
} /* parser_stack_pop_uint8 */
/**
* Pushes an uint16_t value onto the stack.
*/
void
parser_stack_push_uint16 (parser_context_t *context_p, /**< context */
uint16_t uint16_value) /**< value pushed onto the stack */
{
if (context_p->stack.last_position + 2 <= PARSER_STACK_PAGE_SIZE)
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
page_p->bytes[context_p->stack.last_position++] = (uint8_t) (uint16_value >> 8);
page_p->bytes[context_p->stack.last_position++] = (uint8_t) uint16_value;
context_p->stack_top_uint8 = (uint8_t) uint16_value;
}
else
{
parser_stack_push_uint8 (context_p, (uint8_t) (uint16_value >> 8));
parser_stack_push_uint8 (context_p, (uint8_t) uint16_value);
}
} /* parser_stack_push_uint16 */
/**
* Pops the last uint16_t value from the stack.
*
* @return the value popped from the stack.
*/
uint16_t
parser_stack_pop_uint16 (parser_context_t *context_p) /**< context */
{
uint32_t value = context_p->stack_top_uint8;
if (context_p->stack.last_position >= 3)
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
value |= ((uint32_t) page_p->bytes[context_p->stack.last_position - 2]) << 8;
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 3];
context_p->stack.last_position -= 2;
}
else
{
parser_stack_pop_uint8 (context_p);
value |= ((uint32_t) context_p->stack_top_uint8) << 8;
parser_stack_pop_uint8 (context_p);
}
return (uint16_t) value;
} /* parser_stack_pop_uint16 */
/**
* Pushes a data onto the stack.
*/
void
parser_stack_push (parser_context_t *context_p, /**< context */
const void *data_p, /**< data pushed onto the stack */
uint32_t length) /**< length of the data */
{
uint32_t fragment_length = PARSER_STACK_PAGE_SIZE - context_p->stack.last_position;
const uint8_t *bytes_p = (const uint8_t *) data_p;
parser_mem_page_t *page_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
context_p->stack_top_uint8 = bytes_p[length - 1];
if (fragment_length > 0)
{
/* Fill the remaining bytes. */
if (fragment_length > length)
{
fragment_length = length;
}
memcpy (context_p->stack.first_p->bytes + context_p->stack.last_position,
bytes_p,
fragment_length);
if (fragment_length == length)
{
context_p->stack.last_position += length;
return;
}
bytes_p += fragment_length;
length -= fragment_length;
}
if (context_p->free_page_p != NULL)
{
page_p = context_p->free_page_p;
context_p->free_page_p = NULL;
}
else
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
}
page_p->next_p = context_p->stack.first_p;
context_p->stack.first_p = page_p;
memcpy (page_p->bytes, bytes_p, length);
context_p->stack.last_position = length;
} /* parser_stack_push */
/**
* Pop bytes from the top of the stack.
*/
void
parser_stack_pop (parser_context_t *context_p, /**< context */
void *data_p, /**< destination buffer, can be NULL */
uint32_t length) /**< length of the data */
{
uint8_t *bytes_p = (uint8_t *) data_p;
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (context_p->stack.last_position > length)
{
context_p->stack.last_position -= length;
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 1];
if (bytes_p != NULL)
{
memcpy (bytes_p, context_p->stack.first_p->bytes + context_p->stack.last_position, length);
}
return;
}
JERRY_ASSERT (page_p->next_p != NULL);
length -= context_p->stack.last_position;
if (bytes_p != NULL)
{
memcpy (bytes_p + length, page_p->bytes, context_p->stack.last_position);
}
context_p->stack.first_p = page_p->next_p;
context_p->stack.last_position = PARSER_STACK_PAGE_SIZE - length;
context_p->stack_top_uint8 = page_p->next_p->bytes[context_p->stack.last_position - 1];
if (bytes_p != NULL && length > 0)
{
memcpy (bytes_p, page_p->next_p->bytes + context_p->stack.last_position, length);
}
JERRY_ASSERT (context_p->stack.last_position > 0);
if (context_p->free_page_p == NULL)
{
context_p->free_page_p = page_p;
}
else
{
parser_free (page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
} /* parser_stack_pop */
/**
* Skip the next n bytes of the stack.
*/
void
parser_stack_iterator_skip (parser_stack_iterator_t *iterator, /**< iterator */
size_t length) /**< number of skipped bytes */
{
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length < iterator->current_position)
{
iterator->current_position -= length;
}
else
{
iterator->current_position = PARSER_STACK_PAGE_SIZE - (length - iterator->current_position);
iterator->current_p = iterator->current_p->next_p;
}
} /* parser_stack_iterator_skip */
/**
* Read bytes from the stack.
*/
void
parser_stack_iterator_read (parser_stack_iterator_t *iterator, /**< iterator */
void *data_p, /**< destination buffer */
size_t length) /**< length of the data */
{
uint8_t *bytes_p = (uint8_t *) data_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length <= iterator->current_position)
{
memcpy (bytes_p,
iterator->current_p->bytes + iterator->current_position - length,
length);
}
else
{
JERRY_ASSERT (iterator->current_p->next_p != NULL);
length -= iterator->current_position;
memcpy (bytes_p + length,
iterator->current_p->bytes,
iterator->current_position);
memcpy (bytes_p,
iterator->current_p->next_p->bytes + PARSER_STACK_PAGE_SIZE - length,
length);
}
} /* parser_stack_iterator_read */
/**
* Write bytes onto the stack.
*/
void
parser_stack_iterator_write (parser_stack_iterator_t *iterator, /**< iterator */
const void *data_p, /**< destination buffer */
size_t length) /**< length of the data */
{
const uint8_t *bytes_p = (const uint8_t *) data_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length <= iterator->current_position)
{
memcpy (iterator->current_p->bytes + iterator->current_position - length,
bytes_p,
length);
}
else
{
JERRY_ASSERT (iterator->current_p->next_p != NULL);
length -= iterator->current_position;
memcpy (iterator->current_p->bytes,
bytes_p + length,
iterator->current_position);
memcpy (iterator->current_p->next_p->bytes + PARSER_STACK_PAGE_SIZE - length,
bytes_p,
length);
}
} /* parser_stack_iterator_write */
/**
* @}
* @}
*/

View file

@ -0,0 +1,678 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_scanner Scanner
* @{
*/
/**
* Scan mode types types.
*/
typedef enum
{
SCAN_MODE_PRIMARY_EXPRESSION, /**< scanning primary expression */
SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW, /**< scanning primary expression after new */
SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */
SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning prymary expression end */
SCAN_MODE_STATEMENT, /**< scanning statement */
SCAN_MODE_FUNCTION_ARGUMENTS, /**< scanning function arguments */
SCAN_MODE_PROPERTY_NAME, /**< scanning property name */
} scan_modes_t;
/**
* Scan stack mode types types.
*/
typedef enum
{
SCAN_STACK_HEAD, /**< head */
SCAN_STACK_PAREN_EXPRESSION, /**< parent expression group */
SCAN_STACK_PAREN_STATEMENT, /**< parent stetement group */
SCAN_STACK_COLON_EXPRESSION, /**< colon expression group */
SCAN_STACK_COLON_STATEMENT, /**< colon statement group*/
SCAN_STACK_SQUARE_BRACKETED_EXPRESSION, /**< square bracketed expression group */
SCAN_STACK_OBJECT_LITERAL, /**< object literal group */
SCAN_STACK_BLOCK_STATEMENT, /**< block statement group */
SCAN_STACK_BLOCK_EXPRESSION, /**< block expression group*/
SCAN_STACK_BLOCK_PROPERTY, /**< block property group */
} scan_stack_modes_t;
/**
* Scan primary expression.
*
* @return true for continue, false for break
*/
static bool
parser_scan_primary_expression (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_KEYW_NEW:
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW;
break;
}
case LEXER_DIVIDE:
case LEXER_ASSIGN_DIVIDE:
{
lexer_construct_regexp_object (context_p, true);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_KEYW_FUNCTION:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_EXPRESSION);
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
break;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_SQUARE_BRACKETED_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_LEFT_BRACE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_OBJECT_LITERAL);
*mode = SCAN_MODE_PROPERTY_NAME;
return true;
}
case LEXER_LITERAL:
case LEXER_KEYW_THIS:
case LEXER_LIT_TRUE:
case LEXER_LIT_FALSE:
case LEXER_LIT_NULL:
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_RIGHT_SQUARE:
{
if (stack_top != SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
parser_stack_pop_uint8 (context_p);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_COMMA:
{
if (stack_top != SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_RIGHT_PAREN:
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
if (stack_top == SCAN_STACK_PAREN_STATEMENT)
{
*mode = SCAN_MODE_STATEMENT;
}
else if (stack_top != SCAN_STACK_PAREN_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
parser_stack_pop_uint8 (context_p);
break;
}
case LEXER_SEMICOLON:
{
/* Needed by for (;;) statements. */
if (stack_top != SCAN_STACK_PAREN_STATEMENT)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
default:
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
}
return false;
} /* parser_scan_primary_expression */
/**
* Scan the tokens after the primary expression.
*
* @return true for break, false for fall through
*/
static bool
parser_scan_post_primary_expression (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_DOT:
{
lexer_scan_identifier (context_p, false);
return true;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_SQUARE_BRACKETED_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
if (!context_p->token.was_newline)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
return true;
}
/* FALLTHRU */
}
default:
{
break;
}
}
return false;
} /* parser_scan_post_primary_expression */
/**
* Scan the tokens after the primary expression.
*
* @return true for continue, false for break
*/
static bool
parser_scan_primary_expression_end (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
lexer_token_type_t end_type, /**< terminator token type */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_COMMA:
{
if (stack_top == SCAN_STACK_OBJECT_LITERAL)
{
*mode = SCAN_MODE_PROPERTY_NAME;
return true;
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_COLON:
{
if (stack_top == SCAN_STACK_COLON_EXPRESSION
|| stack_top == SCAN_STACK_COLON_STATEMENT)
{
if (stack_top == SCAN_STACK_COLON_EXPRESSION)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
}
else
{
*mode = SCAN_MODE_STATEMENT;
}
parser_stack_pop_uint8 (context_p);
return false;
}
/* FALLTHRU */
}
default:
{
break;
}
}
if (LEXER_IS_BINARY_OP_TOKEN (type)
|| (type == LEXER_SEMICOLON && stack_top == SCAN_STACK_PAREN_STATEMENT))
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
if ((type == LEXER_RIGHT_SQUARE && stack_top == SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
|| (type == LEXER_RIGHT_PAREN && stack_top == SCAN_STACK_PAREN_EXPRESSION)
|| (type == LEXER_RIGHT_BRACE && stack_top == SCAN_STACK_OBJECT_LITERAL))
{
parser_stack_pop_uint8 (context_p);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
return false;
}
*mode = SCAN_MODE_STATEMENT;
if (type == LEXER_RIGHT_PAREN && stack_top == SCAN_STACK_PAREN_STATEMENT)
{
parser_stack_pop_uint8 (context_p);
return false;
}
/* Check whether we can enter to statement mode. */
if (stack_top != SCAN_STACK_BLOCK_STATEMENT
&& stack_top != SCAN_STACK_BLOCK_EXPRESSION
&& !(stack_top == SCAN_STACK_HEAD && end_type == LEXER_SCAN_SWITCH))
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPRESSION);
}
if (type == LEXER_RIGHT_BRACE
|| context_p->token.was_newline)
{
return true;
}
if (type != LEXER_SEMICOLON)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPRESSION);
}
return false;
} /* parser_scan_primary_expression_end */
/**
* Scan statements.
*
* @return true for continue, false for break
*/
static bool
parser_scan_statement (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_SEMICOLON:
case LEXER_KEYW_ELSE:
case LEXER_KEYW_DO:
case LEXER_KEYW_TRY:
case LEXER_KEYW_FINALLY:
case LEXER_KEYW_DEBUGGER:
{
return false;
}
case LEXER_KEYW_IF:
case LEXER_KEYW_WHILE:
case LEXER_KEYW_WITH:
case LEXER_KEYW_SWITCH:
case LEXER_KEYW_CATCH:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_KEYW_FOR:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (context_p->token.type == LEXER_KEYW_VAR)
{
return false;
}
return true;
}
case LEXER_KEYW_VAR:
case LEXER_KEYW_THROW:
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_KEYW_RETURN:
{
lexer_next_token (context_p);
if (!context_p->token.was_newline
&& context_p->token.type != LEXER_SEMICOLON)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
}
return true;
}
case LEXER_KEYW_BREAK:
case LEXER_KEYW_CONTINUE:
{
lexer_next_token (context_p);
if (!context_p->token.was_newline
&& context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
return false;
}
return true;
}
case LEXER_KEYW_DEFAULT:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
{
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
}
return false;
}
case LEXER_KEYW_CASE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_RIGHT_BRACE:
{
if (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
parser_stack_pop_uint8 (context_p);
if (stack_top == SCAN_STACK_BLOCK_EXPRESSION)
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
else if (stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COMMA
&& context_p->token.type != LEXER_RIGHT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED);
}
return true;
}
return false;
}
break;
}
case LEXER_LEFT_BRACE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_STATEMENT);
return false;
}
case LEXER_KEYW_FUNCTION:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_STATEMENT);
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
return false;
}
default:
{
break;
}
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
lexer_next_token (context_p);
if (context_p->token.type == LEXER_COLON)
{
*mode = SCAN_MODE_STATEMENT;
return false;
}
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
return true;
} /* parser_scan_statement */
/**
* Pre-scan for token(s).
*/
void
parser_scan_until (parser_context_t *context_p, /**< context */
lexer_range_t *range_p, /**< destination range */
lexer_token_type_t end_type) /**< terminator token type */
{
scan_modes_t mode;
lexer_token_type_t end_type_b = end_type;
range_p->source_p = context_p->source_p;
range_p->source_end_p = context_p->source_p;
range_p->line = context_p->line;
range_p->column = context_p->column;
mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (end_type == LEXER_KEYW_CASE)
{
end_type = LEXER_SCAN_SWITCH;
end_type_b = LEXER_SCAN_SWITCH;
mode = SCAN_MODE_STATEMENT;
}
else
{
lexer_next_token (context_p);
if (end_type == LEXER_KEYW_IN)
{
end_type_b = LEXER_SEMICOLON;
if (context_p->token.type == LEXER_KEYW_VAR)
{
lexer_next_token (context_p);
}
}
}
parser_stack_push_uint8 (context_p, SCAN_STACK_HEAD);
while (true)
{
lexer_token_type_t type = (lexer_token_type_t) context_p->token.type;
scan_stack_modes_t stack_top = (scan_stack_modes_t) context_p->stack_top_uint8;
if (type == LEXER_EOS)
{
parser_raise_error (context_p, PARSER_ERR_EXPRESSION_EXPECTED);
}
if (stack_top == SCAN_STACK_HEAD
&& (type == end_type || type == end_type_b))
{
parser_stack_pop_uint8 (context_p);
return;
}
switch (mode)
{
case SCAN_MODE_PRIMARY_EXPRESSION:
{
if (type == LEXER_ADD
|| type == LEXER_SUBTRACT
|| LEXER_IS_UNARY_OP_TOKEN (type))
{
break;
}
/* FALLTHRU */
}
case SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW:
{
if (parser_scan_primary_expression (context_p, type, stack_top, &mode))
{
continue;
}
break;
}
case SCAN_MODE_POST_PRIMARY_EXPRESSION:
{
if (parser_scan_post_primary_expression (context_p, type, &mode))
{
break;
}
/* FALLTHRU */
}
case SCAN_MODE_PRIMARY_EXPRESSION_END:
{
if (parser_scan_primary_expression_end (context_p, type, stack_top, end_type, &mode))
{
continue;
}
break;
}
case SCAN_MODE_STATEMENT:
{
if (end_type == LEXER_SCAN_SWITCH
&& stack_top == SCAN_STACK_HEAD
&& (type == LEXER_KEYW_DEFAULT || type == LEXER_KEYW_CASE || type == LEXER_RIGHT_BRACE))
{
parser_stack_pop_uint8 (context_p);
return;
}
if (parser_scan_statement (context_p, type, stack_top, &mode))
{
continue;
}
break;
}
case SCAN_MODE_FUNCTION_ARGUMENTS:
{
JERRY_ASSERT (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);
if (context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
lexer_next_token (context_p);
}
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
while (true)
{
if (context_p->token.type != LEXER_LITERAL
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COMMA)
{
break;
}
lexer_next_token (context_p);
}
}
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
mode = SCAN_MODE_STATEMENT;
break;
}
case SCAN_MODE_PROPERTY_NAME:
{
JERRY_ASSERT (stack_top == SCAN_STACK_OBJECT_LITERAL);
lexer_scan_identifier (context_p, true);
if (context_p->token.type == LEXER_RIGHT_BRACE)
{
parser_stack_pop_uint8 (context_p);
mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
if (context_p->token.type == LEXER_PROPERTY_GETTER
|| context_p->token.type == LEXER_PROPERTY_SETTER)
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_PROPERTY);
mode = SCAN_MODE_FUNCTION_ARGUMENTS;
break;
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
{
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
}
mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
}
range_p->source_end_p = context_p->source_p;
lexer_next_token (context_p);
}
} /* parser_scan_until */
/**
* @}
* @}
* @}
*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,940 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/**********************************************************************/
/* Emitting byte codes */
/**********************************************************************/
/**
* Append two bytes to the cbc stream.
*/
static void
parser_emit_two_bytes (parser_context_t *context_p, /**< context */
uint8_t first_byte, /**< first byte */
uint8_t second_byte) /**< second byte */
{
uint32_t last_position = context_p->byte_code.last_position;
if (last_position + 2 <= PARSER_CBC_STREAM_PAGE_SIZE)
{
parser_mem_page_t *page_p = context_p->byte_code.last_p;
page_p->bytes[last_position] = first_byte;
page_p->bytes[last_position + 1] = second_byte;
context_p->byte_code.last_position = last_position + 2;
}
else if (last_position >= PARSER_CBC_STREAM_PAGE_SIZE)
{
parser_mem_page_t *page_p;
parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
page_p = context_p->byte_code.last_p;
page_p->bytes[0] = first_byte;
page_p->bytes[1] = second_byte;
context_p->byte_code.last_position = 2;
}
else
{
context_p->byte_code.last_p->bytes[PARSER_CBC_STREAM_PAGE_SIZE - 1] = first_byte;
parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
context_p->byte_code.last_p->bytes[0] = second_byte;
context_p->byte_code.last_position = 1;
}
} /* parser_emit_two_bytes */
#define PARSER_APPEND_TO_BYTE_CODE(context_p, byte) \
if ((context_p)->byte_code.last_position >= PARSER_CBC_STREAM_PAGE_SIZE) \
{ \
parser_cbc_stream_alloc_page ((context_p), &(context_p)->byte_code); \
} \
(context_p)->byte_code.last_p->bytes[(context_p)->byte_code.last_position++] = (uint8_t) (byte)
/**
* Append the current byte code to the stream
*/
void
parser_flush_cbc (parser_context_t *context_p) /**< context */
{
uint8_t flags;
if (context_p->last_cbc_opcode == PARSER_CBC_UNAVAILABLE)
{
return;
}
context_p->status_flags |= PARSER_NO_END_LABEL;
if (PARSER_IS_BASIC_OPCODE (context_p->last_cbc_opcode))
{
cbc_opcode_t opcode = (cbc_opcode_t) context_p->last_cbc_opcode;
flags = cbc_flags[opcode];
PARSER_APPEND_TO_BYTE_CODE (context_p, opcode);
context_p->byte_code_size++;
}
else
{
cbc_ext_opcode_t opcode = (cbc_ext_opcode_t) PARSER_GET_EXT_OPCODE (context_p->last_cbc_opcode);
flags = cbc_ext_flags[opcode];
parser_emit_two_bytes (context_p, CBC_EXT_OPCODE, opcode);
context_p->byte_code_size += 2;
}
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
{
uint16_t literal_index = context_p->last_cbc.literal_index;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
}
if (flags & CBC_HAS_LITERAL_ARG2)
{
uint16_t literal_index = context_p->last_cbc.value;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
if (!(flags & CBC_HAS_LITERAL_ARG))
{
literal_index = context_p->last_cbc.third_literal_index;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
}
}
if (flags & CBC_HAS_BYTE_ARG)
{
uint8_t byte_argument = (uint8_t) context_p->last_cbc.value;
JERRY_ASSERT (context_p->last_cbc.value <= CBC_MAXIMUM_BYTE_VALUE);
if (flags & CBC_POP_STACK_BYTE_ARG)
{
JERRY_ASSERT (context_p->stack_depth >= byte_argument);
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, byte_argument);
}
PARSER_APPEND_TO_BYTE_CODE (context_p, byte_argument);
context_p->byte_code_size++;
}
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
const char *name_p;
if (PARSER_IS_BASIC_OPCODE (context_p->last_cbc_opcode))
{
name_p = cbc_names[context_p->last_cbc_opcode];
}
else
{
name_p = cbc_ext_names[PARSER_GET_EXT_OPCODE (context_p->last_cbc_opcode)];
}
JERRY_DEBUG_MSG (" [%3d] %s", (int) context_p->stack_depth, name_p);
if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
{
uint16_t literal_index = context_p->last_cbc.literal_index;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
}
if (flags & CBC_HAS_LITERAL_ARG2)
{
uint16_t literal_index = context_p->last_cbc.value;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
if (!(flags & CBC_HAS_LITERAL_ARG))
{
literal_index = context_p->last_cbc.third_literal_index;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
}
}
if (flags & CBC_HAS_BYTE_ARG)
{
JERRY_DEBUG_MSG (" byte_arg:%d", (int) context_p->last_cbc.value);
}
JERRY_DEBUG_MSG ("\n");
}
#endif /* PARSER_DUMP_BYTE_CODE */
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
} /* parser_flush_cbc */
/**
* Append a byte code
*/
void
parser_emit_cbc (parser_context_t *context_p, /**< context */
uint16_t opcode) /**< opcode */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, 0));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
} /* parser_emit_cbc */
/**
* Append a byte code with a literal argument
*/
void
parser_emit_cbc_literal (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
uint16_t literal_index) /**< literal index */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.literal_index = literal_index;
context_p->last_cbc.literal_type = LEXER_UNUSED_LITERAL;
context_p->last_cbc.literal_object_type = LEXER_LITERAL_OBJECT_ANY;
} /* parser_emit_cbc_literal */
/**
* Append a byte code with the current literal argument
*/
void
parser_emit_cbc_literal_from_token (parser_context_t *context_p, /**< context */
uint16_t opcode) /**< opcode */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.literal_index = context_p->lit_object.index;
context_p->last_cbc.literal_type = context_p->token.lit_location.type;
context_p->last_cbc.literal_object_type = context_p->lit_object.type;
} /* parser_emit_cbc_literal_from_token */
/**
* Append a byte code with a call argument
*/
void
parser_emit_cbc_call (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
size_t call_arguments) /**< number of arguments */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_BYTE_ARG));
JERRY_ASSERT (call_arguments <= CBC_MAXIMUM_BYTE_VALUE);
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.value = (uint16_t) call_arguments;
} /* parser_emit_cbc_call */
/**
* Append a push number 1/2 byte code
*/
void
parser_emit_cbc_push_number (parser_context_t *context_p, /**< context */
bool is_negative_number) /**< sign is negative */
{
uint16_t value = context_p->lit_object.index;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
cbc_opcode_t opcode = is_negative_number ? CBC_PUSH_NUMBER_NEG_BYTE : CBC_PUSH_NUMBER_POS_BYTE;
JERRY_ASSERT (value > 0 && value <= CBC_PUSH_NUMBER_BYTE_RANGE_END);
JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (cbc_flags[opcode]) == 1);
context_p->stack_depth++;
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
int real_value = value;
if (is_negative_number)
{
real_value = -real_value;
}
JERRY_DEBUG_MSG (" [%3d] %s number:%d\n",
(int) context_p->stack_depth,
cbc_names[opcode],
real_value);
}
#endif /* PARSER_DUMP_BYTE_CODE */
parser_emit_two_bytes (context_p, opcode, (uint8_t) (value - 1));
context_p->byte_code_size += 2;
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
} /* parser_emit_cbc_push_number */
/**
* Append a byte code with a branch argument
*/
void
parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
parser_branch_t *branch_p) /**< branch result */
{
uint8_t flags;
uint32_t extra_byte_code_increase;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags |= PARSER_NO_END_LABEL;
if (PARSER_IS_BASIC_OPCODE (opcode))
{
flags = cbc_flags[opcode];
extra_byte_code_increase = 0;
}
else
{
PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);
flags = cbc_ext_flags[opcode];
extra_byte_code_increase = 1;
}
JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
JERRY_ASSERT (CBC_BRANCH_IS_FORWARD (flags));
JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);
/* Branch opcodes never push anything onto the stack. */
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
if (extra_byte_code_increase == 0)
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, cbc_names[opcode]);
}
else
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, cbc_ext_names[opcode]);
}
}
#endif /* PARSER_DUMP_BYTE_CODE */
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
opcode++;
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
PARSER_PLUS_EQUAL_U16 (opcode, 2);
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
parser_emit_two_bytes (context_p, (uint8_t) opcode, 0);
branch_p->page_p = context_p->byte_code.last_p;
branch_p->offset = (context_p->byte_code.last_position - 1) | (context_p->byte_code_size << 8);
context_p->byte_code_size += extra_byte_code_increase;
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
PARSER_APPEND_TO_BYTE_CODE (context_p, 0);
context_p->byte_code_size += 3;
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
parser_emit_two_bytes (context_p, 0, 0);
context_p->byte_code_size += 4;
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
} /* parser_emit_cbc_forward_branch */
/**
* Append a branch byte code and create an item
*/
parser_branch_node_t *
parser_emit_cbc_forward_branch_item (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
parser_branch_node_t *next_p) /**< next branch */
{
parser_branch_t branch;
parser_branch_node_t *new_item;
/* Since byte code insertion may throw an out-of-memory error,
* the branch is constructed locally, and copied later. */
parser_emit_cbc_forward_branch (context_p, opcode, &branch);
new_item = (parser_branch_node_t *) parser_malloc (context_p, sizeof (parser_branch_node_t));
new_item->branch = branch;
new_item->next_p = next_p;
return new_item;
} /* parser_emit_cbc_forward_branch_item */
/**
* Append a byte code with a branch argument
*/
void
parser_emit_cbc_backward_branch (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
uint32_t offset) /**< destination offset */
{
uint8_t flags;
#ifdef PARSER_DUMP_BYTE_CODE
const char *name;
#endif /* PARSER_DUMP_BYTE_CODE */
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags |= PARSER_NO_END_LABEL;
offset = context_p->byte_code_size - offset;
if (PARSER_IS_BASIC_OPCODE (opcode))
{
flags = cbc_flags[opcode];
#ifdef PARSER_DUMP_BYTE_CODE
name = cbc_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
}
else
{
PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);
flags = cbc_ext_flags[opcode];
context_p->byte_code_size++;
#ifdef PARSER_DUMP_BYTE_CODE
name = cbc_ext_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
}
JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
JERRY_ASSERT (CBC_BRANCH_IS_BACKWARD (flags));
JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);
JERRY_ASSERT (offset <= context_p->byte_code_size);
/* Branch opcodes never push anything onto the stack. */
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, name);
}
#endif /* PARSER_DUMP_BYTE_CODE */
context_p->byte_code_size += 2;
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
if (offset > 255)
{
opcode++;
context_p->byte_code_size++;
}
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
if (offset > 65535)
{
PARSER_PLUS_EQUAL_U16 (opcode, 2);
context_p->byte_code_size += 2;
}
else if (offset > 255)
{
opcode++;
context_p->byte_code_size++;
}
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
PARSER_APPEND_TO_BYTE_CODE (context_p, (uint8_t) opcode);
#if PARSER_MAXIMUM_CODE_SIZE > 65535
if (offset > 65535)
{
PARSER_APPEND_TO_BYTE_CODE (context_p, offset >> 16);
}
#endif /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
if (offset > 255)
{
PARSER_APPEND_TO_BYTE_CODE (context_p, (offset >> 8) & 0xff);
}
PARSER_APPEND_TO_BYTE_CODE (context_p, offset & 0xff);
} /* parser_emit_cbc_backward_branch */
#undef PARSER_CHECK_LAST_POSITION
#undef PARSER_APPEND_TO_BYTE_CODE
/**
* Set a branch to the current byte code position
*/
void
parser_set_branch_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_t *branch_p) /**< branch result */
{
uint32_t delta;
size_t offset;
parser_mem_page_t *page_p = branch_p->page_p;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags &= ~PARSER_NO_END_LABEL;
JERRY_ASSERT (context_p->byte_code_size > (branch_p->offset >> 8));
delta = context_p->byte_code_size - (branch_p->offset >> 8);
offset = (branch_p->offset & CBC_LOWER_SEVEN_BIT_MASK);
JERRY_ASSERT (delta <= PARSER_MAXIMUM_CODE_SIZE);
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
page_p->bytes[offset++] = (uint8_t) (delta >> 8);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
page_p->bytes[offset++] = (uint8_t) (delta >> 16);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
page_p->bytes[offset++] = (uint8_t) ((delta >> 8) & 0xff);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
page_p->bytes[offset++] = delta & 0xff;
} /* parser_set_branch_to_current_position */
/**
* Set breaks to the current byte code position
*/
void
parser_set_breaks_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_node_t *current_p) /**< branch list */
{
while (current_p != NULL)
{
parser_branch_node_t *next_p = current_p->next_p;
if (!(current_p->branch.offset & CBC_HIGHEST_BIT_MASK))
{
parser_set_branch_to_current_position (context_p, &current_p->branch);
}
parser_free (current_p, sizeof (parser_branch_node_t));
current_p = next_p;
}
} /* parser_set_breaks_to_current_position */
/**
* Set continues to the current byte code position
*/
void
parser_set_continues_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_node_t *current_p) /**< branch list */
{
while (current_p != NULL)
{
if (current_p->branch.offset & CBC_HIGHEST_BIT_MASK)
{
parser_set_branch_to_current_position (context_p, &current_p->branch);
}
current_p = current_p->next_p;
}
} /* parser_set_continues_to_current_position */
#if JERRY_ENABLE_ERROR_MESSAGES
/**
* Returns with the string representation of the error
*/
const char *
parser_error_to_string (parser_error_t error) /**< error code */
{
switch (error)
{
case PARSER_ERR_OUT_OF_MEMORY:
{
return "Out of memory.";
}
case PARSER_ERR_LITERAL_LIMIT_REACHED:
{
return "Maximum number of literals reached.";
}
case PARSER_ERR_ARGUMENT_LIMIT_REACHED:
{
return "Maximum number of function arguments reached.";
}
case PARSER_ERR_STACK_LIMIT_REACHED:
{
return "Maximum function stack size reached.";
}
case PARSER_ERR_REGISTER_LIMIT_REACHED:
{
return "Maximum number of registers is reached.";
}
case PARSER_ERR_INVALID_CHARACTER:
{
return "Invalid (unexpected) character.";
}
case PARSER_ERR_INVALID_HEX_DIGIT:
{
return "Invalid hexadecimal digit.";
}
case PARSER_ERR_INVALID_ESCAPE_SEQUENCE:
{
return "Invalid escape sequence.";
}
case PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE:
{
return "Invalid unicode escape sequence.";
}
case PARSER_ERR_INVALID_IDENTIFIER_START:
{
return "Character cannot be start of an identifier.";
}
case PARSER_ERR_INVALID_IDENTIFIER_PART:
{
return "Character cannot be part of an identifier.";
}
case PARSER_ERR_INVALID_NUMBER:
{
return "Invalid number.";
}
case PARSER_ERR_MISSING_EXPONENT:
{
return "Missing exponent part.";
}
case PARSER_ERR_IDENTIFIER_AFTER_NUMBER:
{
return "Identifier cannot start after a number.";
}
case PARSER_ERR_INVALID_REGEXP:
{
return "Invalid regular expression.";
}
case PARSER_ERR_UNKNOWN_REGEXP_FLAG:
{
return "Unknown regexp flag.";
}
case PARSER_ERR_DUPLICATED_REGEXP_FLAG:
{
return "Duplicated regexp flag.";
}
case PARSER_ERR_UNSUPPORTED_REGEXP:
{
return "Regexp is not supported in the selected profile.";
}
case PARSER_ERR_IDENTIFIER_TOO_LONG:
{
return "Identifier is too long.";
}
case PARSER_ERR_STRING_TOO_LONG:
{
return "String is too long.";
}
case PARSER_ERR_NUMBER_TOO_LONG:
{
return "Number too long.";
}
case PARSER_ERR_REGEXP_TOO_LONG:
{
return "Regexp too long.";
}
case PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT:
{
return "Unterminated multiline comment.";
}
case PARSER_ERR_UNTERMINATED_STRING:
{
return "Unterminated string literal.";
}
case PARSER_ERR_UNTERMINATED_REGEXP:
{
return "Unterminated regexp literal.";
}
case PARSER_ERR_NEWLINE_NOT_ALLOWED:
{
return "Newline is not allowed in strings or regexps.";
}
case PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED:
{
return "Octal numbers are not allowed in strict mode.";
}
case PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED:
{
return "Octal escape sequences are not allowed in strict mode.";
}
case PARSER_ERR_STRICT_IDENT_NOT_ALLOWED:
{
return "Identifier name is reserved in strict mode.";
}
case PARSER_ERR_EVAL_NOT_ALLOWED:
{
return "Eval is not allowed to use here in strict mode.";
}
case PARSER_ERR_ARGUMENTS_NOT_ALLOWED:
{
return "Arguments is not allowed to use here in strict mode.";
}
case PARSER_ERR_DELETE_IDENT_NOT_ALLOWED:
{
return "Deleting identifier is not allowed in strict mode.";
}
case PARSER_ERR_EVAL_CANNOT_ASSIGNED:
{
return "Eval cannot assigned in strict mode.";
}
case PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED:
{
return "Arguments cannot assigned in strict mode.";
}
case PARSER_ERR_WITH_NOT_ALLOWED:
{
return "With statement not allowed in strict mode.";
}
case PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED:
{
return "Multiple default cases not allowed.";
}
case PARSER_ERR_DEFAULT_NOT_IN_SWITCH:
{
return "Default statement must be in a switch block.";
}
case PARSER_ERR_CASE_NOT_IN_SWITCH:
{
return "Case statement must be in a switch block.";
}
case PARSER_ERR_LEFT_PAREN_EXPECTED:
{
return "Expected '(' token.";
}
case PARSER_ERR_LEFT_BRACE_EXPECTED:
{
return "Expected '{' token.";
}
case PARSER_ERR_RIGHT_PAREN_EXPECTED:
{
return "Expected ')' token.";
}
case PARSER_ERR_RIGHT_SQUARE_EXPECTED:
{
return "Expected ']' token.";
}
case PARSER_ERR_COLON_EXPECTED:
{
return "Expected ':' token.";
}
case PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED:
{
return "Expected ':' token for ?: conditional expression.";
}
case PARSER_ERR_SEMICOLON_EXPECTED:
{
return "Expected ';' token.";
}
case PARSER_ERR_IN_EXPECTED:
{
return "Expected 'in' token.";
}
case PARSER_ERR_WHILE_EXPECTED:
{
return "While expected for do-while loop.";
}
case PARSER_ERR_CATCH_FINALLY_EXPECTED:
{
return "Catch or finally block expected.";
}
case PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED:
{
return "Expected ',' or ']' after an array item.";
}
case PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED:
{
return "Expected ',' or '}' after a property definition.";
}
case PARSER_ERR_IDENTIFIER_EXPECTED:
{
return "Identifier expected.";
}
case PARSER_ERR_EXPRESSION_EXPECTED:
{
return "Expression expected.";
}
case PARSER_ERR_PRIMARY_EXP_EXPECTED:
{
return "Primary expression expected.";
}
case PARSER_ERR_STATEMENT_EXPECTED:
{
return "Statement expected.";
}
case PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED:
{
return "Property identifier expected.";
}
case PARSER_ERR_ARGUMENT_LIST_EXPECTED:
{
return "Expected argument list.";
}
case PARSER_ERR_NO_ARGUMENTS_EXPECTED:
{
return "Property getters must have no arguments.";
}
case PARSER_ERR_ONE_ARGUMENT_EXPECTED:
{
return "Property setters must have one argument.";
}
case PARSER_ERR_INVALID_EXPRESSION:
{
return "Invalid expression.";
}
case PARSER_ERR_INVALID_SWITCH:
{
return "Invalid switch body.";
}
case PARSER_ERR_INVALID_BREAK:
{
return "Break statement must be inside a loop or switch.";
}
case PARSER_ERR_INVALID_BREAK_LABEL:
{
return "Labelled statement targeted by a break not found.";
}
case PARSER_ERR_INVALID_CONTINUE:
{
return "Continue statement must be inside a loop.";
}
case PARSER_ERR_INVALID_CONTINUE_LABEL:
{
return "Labelled statement targeted by a continue noty found.";
}
case PARSER_ERR_INVALID_RETURN:
{
return "Return statement must be inside a function body.";
}
case PARSER_ERR_INVALID_RIGHT_SQUARE:
{
return "Unexpected '}' token.";
}
case PARSER_ERR_DUPLICATED_LABEL:
{
return "Duplicated label.";
}
case PARSER_ERR_OBJECT_PROPERTY_REDEFINED:
{
return "Property of object literal redefined.";
}
case PARSER_ERR_NON_STRICT_ARG_DEFINITION:
{
return "Non strict argument definition.";
}
default:
{
JERRY_ASSERT (error == PARSER_ERR_NO_ERROR);
return "No error.";
}
}
} /* parser_error_to_string */
#endif /* JERRY_ENABLE_ERROR_MESSAGES */
/**
* @}
* @}
* @}
*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,142 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* 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 JS_PARSER_H
#define JS_PARSER_H
#include "ecma-globals.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_parser Parser
* @{
*/
/**
* Error codes.
*/
typedef enum
{
PARSER_ERR_NO_ERROR, /**< no error */
PARSER_ERR_OUT_OF_MEMORY, /**< out of memory */
PARSER_ERR_LITERAL_LIMIT_REACHED, /**< maximum number of literals reached */
PARSER_ERR_ARGUMENT_LIMIT_REACHED, /**< maximum number of function arguments reached */
PARSER_ERR_STACK_LIMIT_REACHED, /**< maximum function stack size reached */
PARSER_ERR_REGISTER_LIMIT_REACHED, /**< maximum register size reached */
PARSER_ERR_INVALID_CHARACTER, /**< unexpected character */
PARSER_ERR_INVALID_HEX_DIGIT, /**< invalid hexadecimal digit */
PARSER_ERR_INVALID_ESCAPE_SEQUENCE, /**< invalid escape sequence */
PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE, /**< invalid unicode escape sequence */
PARSER_ERR_INVALID_IDENTIFIER_START, /**< character cannot be start of an identifier */
PARSER_ERR_INVALID_IDENTIFIER_PART, /**< character cannot be part of an identifier */
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
PARSER_ERR_IDENTIFIER_AFTER_NUMBER, /**< identifier start after number */
PARSER_ERR_INVALID_REGEXP, /**< invalid regular expression */
PARSER_ERR_UNKNOWN_REGEXP_FLAG, /**< unknown regexp flag */
PARSER_ERR_DUPLICATED_REGEXP_FLAG, /**< duplicated regexp flag */
PARSER_ERR_UNSUPPORTED_REGEXP, /**< regular expression is not supported */
PARSER_ERR_IDENTIFIER_TOO_LONG, /**< too long identifier */
PARSER_ERR_STRING_TOO_LONG, /**< too long string literal */
PARSER_ERR_NUMBER_TOO_LONG, /**< too long number literal */
PARSER_ERR_REGEXP_TOO_LONG, /**< too long regexp literal */
PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT, /**< unterminated multiline comment */
PARSER_ERR_UNTERMINATED_STRING, /**< unterminated string literal */
PARSER_ERR_UNTERMINATED_REGEXP, /**< unterminated regexp literal */
PARSER_ERR_NEWLINE_NOT_ALLOWED, /**< newline is not allowed */
PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED, /**< octal numbers are not allowed in strict mode */
PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED, /**< octal escape sequences are not allowed in strict mode */
PARSER_ERR_STRICT_IDENT_NOT_ALLOWED, /**< identifier name is reserved in strict mode */
PARSER_ERR_EVAL_NOT_ALLOWED, /**< eval is not allowed here in strict mode */
PARSER_ERR_ARGUMENTS_NOT_ALLOWED, /**< arguments is not allowed here in strict mode */
PARSER_ERR_DELETE_IDENT_NOT_ALLOWED, /**< identifier delete is not allowed in strict mode */
PARSER_ERR_EVAL_CANNOT_ASSIGNED, /**< eval cannot be assigned in strict mode */
PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED, /**< arguments cannot be assigned in strict mode */
PARSER_ERR_WITH_NOT_ALLOWED, /**< with statement is not allowed in strict mode */
PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED, /**< multiple default cases are not allowed */
PARSER_ERR_DEFAULT_NOT_IN_SWITCH, /**< default statement is not in switch block */
PARSER_ERR_CASE_NOT_IN_SWITCH, /**< case statement is not in switch block */
PARSER_ERR_LEFT_PAREN_EXPECTED, /**< left paren expected */
PARSER_ERR_LEFT_BRACE_EXPECTED, /**< left brace expected */
PARSER_ERR_RIGHT_PAREN_EXPECTED, /**< right paren expected */
PARSER_ERR_RIGHT_SQUARE_EXPECTED, /**< right square expected */
PARSER_ERR_COLON_EXPECTED, /**< colon expected */
PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED, /**< colon expected for conditional expression */
PARSER_ERR_SEMICOLON_EXPECTED, /**< semicolon expected */
PARSER_ERR_IN_EXPECTED, /**< in keyword expected */
PARSER_ERR_WHILE_EXPECTED, /**< while expected for do-while loop */
PARSER_ERR_CATCH_FINALLY_EXPECTED, /**< catch or finally expected */
PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED, /**< array item separator expected */
PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED, /**< object item separator expected */
PARSER_ERR_IDENTIFIER_EXPECTED, /**< identifier expected */
PARSER_ERR_EXPRESSION_EXPECTED, /**< expression expected */
PARSER_ERR_PRIMARY_EXP_EXPECTED, /**< primary expression expected */
PARSER_ERR_STATEMENT_EXPECTED, /**< statement expected */
PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED, /**< property identifier expected */
PARSER_ERR_ARGUMENT_LIST_EXPECTED, /**< argument list expected */
PARSER_ERR_NO_ARGUMENTS_EXPECTED, /**< property getters must have no arguments */
PARSER_ERR_ONE_ARGUMENT_EXPECTED, /**< property setters must have one argument */
PARSER_ERR_INVALID_EXPRESSION, /**< invalid expression */
PARSER_ERR_INVALID_SWITCH, /**< invalid switch body */
PARSER_ERR_INVALID_BREAK, /**< break must be inside a loop or switch */
PARSER_ERR_INVALID_BREAK_LABEL, /**< break target not found */
PARSER_ERR_INVALID_CONTINUE, /**< continue must be inside a loop */
PARSER_ERR_INVALID_CONTINUE_LABEL, /**< continue target not found */
PARSER_ERR_INVALID_RETURN, /**< return must be inside a function */
PARSER_ERR_INVALID_RIGHT_SQUARE, /**< right square must terminate a block */
PARSER_ERR_DUPLICATED_LABEL, /**< duplicated label */
PARSER_ERR_OBJECT_PROPERTY_REDEFINED, /**< property of object literal redefined */
PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */
} parser_error_t;
/* Source code line counter type. */
typedef uint32_t parser_line_counter_t;
/**
* Error code location.
*/
typedef struct
{
parser_error_t error; /**< error code */
parser_line_counter_t line; /**< line where the error occured */
parser_line_counter_t column; /**< column where the error occured */
} parser_error_location_t;
/* Note: source must be a valid UTF-8 string */
extern ecma_value_t parser_parse_script (const uint8_t *, size_t, bool, ecma_compiled_code_t **);
const char *parser_error_to_string (parser_error_t);
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_H */