1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef VM_STACK_H
17#define VM_STACK_H
18
19#include "ecma-globals.h"
20
21/** \addtogroup vm Virtual machine
22 * @{
23 *
24 * \addtogroup stack VM stack
25 * @{
26 */
27
28/**
29 * Create context on the vm stack.
30 */
31#define VM_CREATE_CONTEXT(type, end_offset) ((ecma_value_t) ((type) | ((end_offset) << 7)))
32
33/**
34 * Create context on the vm stack with environment.
35 */
36#define VM_CREATE_CONTEXT_WITH_ENV(type, end_offset) \
37  (VM_CREATE_CONTEXT ((type),(end_offset)) | VM_CONTEXT_HAS_LEX_ENV)
38
39/**
40 * Get type of a vm context.
41 */
42#define VM_GET_CONTEXT_TYPE(value) ((vm_stack_context_type_t) ((value) & 0x1f))
43
44/**
45 * Get the end position of a vm context.
46 */
47#define VM_GET_CONTEXT_END(value) ((value) >> 7)
48
49/**
50 * This flag is set if the context has a lexical environment.
51 */
52#define VM_CONTEXT_HAS_LEX_ENV 0x20
53
54/**
55 * This flag is set if the iterator close operation should be invoked during a for-of context break.
56 */
57#define VM_CONTEXT_CLOSE_ITERATOR 0x40
58
59/**
60 * Context types for the vm stack.
61 */
62typedef enum
63{
64  /* Update VM_CONTEXT_IS_FINALLY macro if the following three values are changed. */
65  VM_CONTEXT_FINALLY_JUMP,                    /**< finally context with a jump */
66  VM_CONTEXT_FINALLY_THROW,                   /**< finally context with a throw */
67  VM_CONTEXT_FINALLY_RETURN,                  /**< finally context with a return */
68  VM_CONTEXT_TRY,                             /**< try context */
69  VM_CONTEXT_CATCH,                           /**< catch context */
70#if ENABLED (JERRY_ES2015)
71  VM_CONTEXT_BLOCK,                           /**< block context */
72#endif /* ENABLED (JERRY_ES2015) */
73  VM_CONTEXT_WITH,                            /**< with context */
74  VM_CONTEXT_FOR_IN,                          /**< for-in context */
75#if ENABLED (JERRY_ES2015)
76  VM_CONTEXT_FOR_OF,                          /**< for-of context */
77#endif /* ENABLED (JERRY_ES2015) */
78} vm_stack_context_type_t;
79
80/**
81 * Checks whether the context type is a finally type.
82 */
83#define VM_CONTEXT_IS_FINALLY(context_type) \
84  ((context_type) <= VM_CONTEXT_FINALLY_RETURN)
85
86/**
87 * Shift needs to be applied to get the next item of the offset array.
88 */
89#define VM_CONTEXT_OFFSET_SHIFT 4
90
91/**
92 * Checks whether an offset is available.
93 */
94#define VM_CONTEXT_HAS_NEXT_OFFSET(offsets) ((offsets) >= (1 << VM_CONTEXT_OFFSET_SHIFT))
95
96/**
97 * Gets the next offset from the offset array.
98 */
99#define VM_CONTEXT_GET_NEXT_OFFSET(offsets) (-((int32_t) ((offsets) & ((1 << VM_CONTEXT_OFFSET_SHIFT) - 1))))
100
101ecma_value_t *vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, ecma_value_t *vm_stack_top_p);
102bool vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, ecma_value_t **vm_stack_top_ref_p,
103                            vm_stack_context_type_t finally_type, uint32_t search_limit);
104uint32_t vm_get_context_value_offsets (ecma_value_t *context_item_p);
105void vm_ref_lex_env_chain (ecma_object_t *lex_env_p, uint16_t context_depth,
106                           ecma_value_t *context_end_p, bool do_ref);
107
108/**
109 * @}
110 * @}
111 */
112
113#endif /* !VM_STACK_H */
114