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 JS_PARSER_INTERNAL_H 17 #define JS_PARSER_INTERNAL_H 18 19 #include "common.h" 20 21 #include "byte-code.h" 22 #include "debugger.h" 23 #include "js-parser.h" 24 #include "js-parser-limits.h" 25 #include "js-lexer.h" 26 #include "js-scanner.h" 27 28 #include "ecma-module.h" 29 30 /** \addtogroup parser Parser 31 * @{ 32 * 33 * \addtogroup jsparser JavaScript 34 * @{ 35 * 36 * \addtogroup jsparser_internals Internals 37 * @{ 38 */ 39 40 /** 41 * General parser flags. 42 */ 43 typedef enum 44 { 45 PARSER_IS_STRICT = (1u << 0), /**< strict mode code */ 46 PARSER_IS_FUNCTION = (1u << 1), /**< function body is parsed */ 47 PARSER_IS_CLOSURE = (1u << 2), /**< function body is encapsulated in {} block */ 48 PARSER_IS_FUNC_EXPRESSION = (1u << 3), /**< a function expression is parsed */ 49 PARSER_IS_PROPERTY_GETTER = (1u << 4), /**< a property getter function is parsed */ 50 PARSER_IS_PROPERTY_SETTER = (1u << 5), /**< a property setter function is parsed */ 51 PARSER_HAS_NON_STRICT_ARG = (1u << 6), /**< the function has arguments which 52 * are not supported in strict mode */ 53 PARSER_ARGUMENTS_NEEDED = (1u << 7), /**< arguments object must be created */ 54 PARSER_LEXICAL_ENV_NEEDED = (1u << 8), /**< lexical environment object must be created */ 55 PARSER_INSIDE_WITH = (1u << 9), /**< code block is inside a with statement */ 56 PARSER_NO_END_LABEL = (1u << 10), /**< return instruction must be inserted 57 * after the last byte code */ 58 PARSER_DEBUGGER_BREAKPOINT_APPENDED = (1u << 11), /**< pending (unsent) breakpoint 59 * info is available */ 60 #if ENABLED (JERRY_ES2015) 61 PARSER_LEXICAL_BLOCK_NEEDED = (1u << 12), /**< global script: needs a lexical environment for let and const 62 * function: needs a lexical environment for arguments */ 63 PARSER_IS_ARROW_FUNCTION = (1u << 13), /**< an arrow function is parsed */ 64 PARSER_IS_GENERATOR_FUNCTION = (1u << 14), /**< a generator function is parsed */ 65 PARSER_IS_ASYNC_FUNCTION = (1u << 15), /**< an async function is parsed */ 66 PARSER_DISALLOW_AWAIT_YIELD = (1u << 16), /**< throw SyntaxError for await / yield keywords */ 67 PARSER_FUNCTION_IS_PARSING_ARGS = (1u << 17), /**< set when parsing function arguments */ 68 PARSER_FUNCTION_HAS_NON_SIMPLE_PARAM = (1u << 18), /**< function has a non simple parameter */ 69 PARSER_FUNCTION_HAS_REST_PARAM = (1u << 19), /**< function has rest parameter */ 70 PARSER_CLASS_CONSTRUCTOR = (1u << 20), /**< a class constructor is parsed 71 * Note: PARSER_ALLOW_SUPER must be present */ 72 /* These three status flags must be in this order. See PARSER_SAVED_FLAGS_OFFSET. */ 73 PARSER_ALLOW_SUPER = (1u << 21), /**< allow super property access */ 74 PARSER_ALLOW_SUPER_CALL = (1u << 22), /**< allow super constructor call 75 * Note: PARSER_CLASS_CONSTRUCTOR must be present */ 76 PARSER_ALLOW_NEW_TARGET = (1u << 23), /**< allow new.target parsing in the current context */ 77 78 #endif /* ENABLED (JERRY_ES2015) */ 79 #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) 80 PARSER_MODULE_DEFAULT_CLASS_OR_FUNC = (1u << 24), /**< parsing a function or class default export */ 81 PARSER_MODULE_STORE_IDENT = (1u << 25), /**< store identifier of the current export statement */ 82 #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ 83 PARSER_HAS_LATE_LIT_INIT = (1u << 30), /**< there are identifier or string literals which construction 84 * is postponed after the local parser data is freed */ 85 #ifndef JERRY_NDEBUG 86 PARSER_SCANNING_SUCCESSFUL = PARSER_HAS_LATE_LIT_INIT, /**< scanning process was successful */ 87 #endif /* !JERRY_NDEBUG */ 88 } parser_general_flags_t; 89 90 /** 91 * Expression parsing flags. 92 */ 93 typedef enum 94 { 95 PARSE_EXPR = 0, /**< parse an expression without any special flags */ 96 PARSE_EXPR_LEFT_HAND_SIDE = (1u << 0), /**< parse a left-hand-side expression */ 97 PARSE_EXPR_NO_PUSH_RESULT = (1u << 1), /**< do not push the result of the expression onto the stack */ 98 PARSE_EXPR_NO_COMMA = (1u << 2), /**< do not parse comma operator */ 99 PARSE_EXPR_HAS_LITERAL = (1u << 3), /**< a primary literal is provided by a 100 * CBC_PUSH_LITERAL instruction */ 101 } parser_expression_flags_t; 102 103 /** 104 * Pattern parsing flags. 105 */ 106 typedef enum 107 { 108 PARSER_PATTERN_NO_OPTS = 0, /**< parse the expression after '=' */ 109 PARSER_PATTERN_BINDING = (1u << 0), /**< parse BindingPattern */ 110 PARSER_PATTERN_TARGET_ON_STACK = (1u << 1), /**< assignment target is the topmost element on the stack */ 111 PARSER_PATTERN_TARGET_DEFAULT = (1u << 2), /**< perform default value comparison for assignment target */ 112 PARSER_PATTERN_NESTED_PATTERN = (1u << 3), /**< parse patter inside a pattern */ 113 PARSER_PATTERN_LET = (1u << 4), /**< pattern is a let declaration */ 114 PARSER_PATTERN_CONST = (1u << 5), /**< pattern is a const declaration */ 115 PARSER_PATTERN_LOCAL = (1u << 6), /**< pattern is a local (catch parameter) declaration */ 116 PARSER_PATTERN_REST_ELEMENT = (1u << 7), /**< parse rest array initializer */ 117 PARSER_PATTERN_ARGUMENTS = (1u << 8), /**< parse arguments binding */ 118 PARSER_PATTERN_ARRAY = (1u << 9), /**< array pattern is being parsed */ 119 } parser_pattern_flags_t; 120 121 /** 122 * Check type for scanner_is_context_needed function. 123 */ 124 typedef enum 125 { 126 PARSER_CHECK_BLOCK_CONTEXT, /**< check block context */ 127 #if ENABLED (JERRY_ES2015) 128 PARSER_CHECK_GLOBAL_CONTEXT, /**< check global context */ 129 PARSER_CHECK_FUNCTION_CONTEXT, /**< check function context */ 130 #endif /* ENABLED (JERRY_ES2015) */ 131 } parser_check_context_type_t; 132 133 /** 134 * Mask for strict mode code 135 */ 136 #define PARSER_STRICT_MODE_MASK 0x1 137 138 /** 139 * Shorthand for function closure definition 140 */ 141 #define PARSER_FUNCTION_CLOSURE (PARSER_IS_FUNCTION | PARSER_IS_CLOSURE) 142 143 #if PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX 144 /** 145 * Maximum number of bytes for branch target. 146 */ 147 #define PARSER_MAX_BRANCH_LENGTH 2 148 #else /* PARSER_MAXIMUM_CODE_SIZE > UINT16_MAX */ 149 /** 150 * Maximum number of bytes for branch target. 151 */ 152 #define PARSER_MAX_BRANCH_LENGTH 3 153 #endif /* PARSER_MAXIMUM_CODE_SIZE <= UINT16_MAX */ 154 155 #if ENABLED (JERRY_ES2015) 156 /** 157 * Offset of PARSER_ALLOW_SUPER 158 */ 159 #define PARSER_SAVED_FLAGS_OFFSET \ 160 JERRY_LOG2 (PARSER_ALLOW_SUPER) 161 162 /** 163 * Mask of saved flags 164 */ 165 #define PARSER_SAVED_FLAGS_MASK \ 166 ((1 << (JERRY_LOG2 (PARSER_ALLOW_NEW_TARGET) - JERRY_LOG2 (PARSER_ALLOW_SUPER) + 1)) - 1) 167 168 /** 169 * Get class option bits from parser_general_flags_t 170 */ 171 #define PARSER_SAVE_STATUS_FLAGS(opts) \ 172 ((uint16_t) (((opts) >> PARSER_SAVED_FLAGS_OFFSET) & PARSER_SAVED_FLAGS_MASK)) 173 174 /** 175 * Mask for get class option bits from ecma_parse_opts_t 176 */ 177 #define PARSER_RESTORE_STATUS_FLAGS_MASK \ 178 (((ECMA_PARSE_ALLOW_NEW_TARGET << 1) - 1) - (ECMA_PARSE_ALLOW_SUPER - 1)) 179 180 /** 181 * Shift for get class option bits from ecma_parse_opts_t 182 */ 183 #define PARSER_RESTORE_STATUS_FLAGS_SHIFT \ 184 (JERRY_LOG2 (PARSER_ALLOW_SUPER) - JERRY_LOG2 (ECMA_PARSE_ALLOW_SUPER)) 185 186 /** 187 * Get class option bits from ecma_parse_opts_t 188 */ 189 #define PARSER_RESTORE_STATUS_FLAGS(opts) \ 190 (((opts) & PARSER_RESTORE_STATUS_FLAGS_MASK) << PARSER_RESTORE_STATUS_FLAGS_SHIFT) 191 192 /** 193 * All flags that affect exotic arguments object creation. 194 */ 195 #define PARSER_ARGUMENTS_RELATED_FLAGS \ 196 (PARSER_ARGUMENTS_NEEDED | PARSER_FUNCTION_HAS_NON_SIMPLE_PARAM | PARSER_IS_STRICT) 197 198 /** 199 * Get the corresponding eval flag for a ecma_parse_opts_t flag 200 */ 201 #define PARSER_GET_EVAL_FLAG(type) \ 202 ((type) >> JERRY_LOG2 (ECMA_PARSE_ALLOW_SUPER)) 203 204 #else /* !ENABLED (JERRY_ES2015) */ 205 206 /** 207 * All flags that affect exotic arguments object creation. 208 */ 209 #define PARSER_ARGUMENTS_RELATED_FLAGS \ 210 (PARSER_ARGUMENTS_NEEDED | PARSER_IS_STRICT) 211 212 #endif /* ENABLED (JERRY_ES2015) */ 213 214 /* Checks whether unmapped arguments are needed. */ 215 #define PARSER_NEEDS_MAPPED_ARGUMENTS(status_flags) \ 216 (((status_flags) & PARSER_ARGUMENTS_RELATED_FLAGS) == PARSER_ARGUMENTS_NEEDED) 217 218 /* The maximum of PARSER_CBC_STREAM_PAGE_SIZE is 127. */ 219 #define PARSER_CBC_STREAM_PAGE_SIZE \ 220 ((uint32_t) (64 - sizeof (void *))) 221 222 /* Defines the size of the max page. */ 223 #define PARSER_STACK_PAGE_SIZE \ 224 ((uint32_t) (((sizeof (void *) > 4) ? 128 : 64) - sizeof (void *))) 225 226 /* Avoid compiler warnings for += operations. */ 227 #define PARSER_PLUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) + (value)) 228 #define PARSER_MINUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) - (value)) 229 #define PARSER_PLUS_EQUAL_LC(base, value) (base) = (parser_line_counter_t) ((base) + (value)) 230 231 /** 232 * Argument for a compact-byte code. 233 */ 234 typedef struct 235 { 236 uint16_t literal_index; /**< literal index argument */ 237 uint16_t value; /**< other argument (second literal or byte). */ 238 uint16_t third_literal_index; /**< literal index argument */ 239 uint8_t literal_type; /**< last literal type */ 240 uint8_t literal_keyword_type; /**< last literal keyword type */ 241 } cbc_argument_t; 242 243 /* Useful parser macros. */ 244 245 #define PARSER_CBC_UNAVAILABLE CBC_EXT_OPCODE 246 247 #define PARSER_TO_EXT_OPCODE(opcode) ((uint16_t) ((opcode) + 256)) 248 #define PARSER_GET_EXT_OPCODE(opcode) ((opcode) - 256) 249 #define PARSER_IS_BASIC_OPCODE(opcode) ((opcode) < 256) 250 #define PARSER_IS_PUSH_LITERAL(opcode) \ 251 ((opcode) == CBC_PUSH_LITERAL \ 252 || (opcode) == CBC_PUSH_TWO_LITERALS \ 253 || (opcode) == CBC_PUSH_THREE_LITERALS) 254 #define PARSER_IS_PUSH_NUMBER(opcode) \ 255 ((opcode) == CBC_PUSH_NUMBER_0 \ 256 || (opcode) == CBC_PUSH_NUMBER_POS_BYTE \ 257 || (opcode) == CBC_PUSH_NUMBER_NEG_BYTE \ 258 || (opcode) == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_0) \ 259 || (opcode) == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_POS_BYTE) \ 260 || (opcode) == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_NEG_BYTE)) 261 262 #define PARSER_IS_MUTABLE_PUSH_LITERAL(opcode) \ 263 ((opcode) >= CBC_PUSH_LITERAL && (opcode) <= CBC_PUSH_THIS_LITERAL) 264 265 #define PARSER_IS_PUSH_LITERALS_WITH_THIS(opcode) \ 266 ((opcode) >= CBC_PUSH_LITERAL && (opcode) <= CBC_PUSH_THREE_LITERALS) 267 268 #define PARSER_IS_PUSH_PROP(opcode) \ 269 ((opcode) >= CBC_PUSH_PROP && (opcode) <= CBC_PUSH_PROP_THIS_LITERAL) 270 271 #define PARSER_IS_PUSH_PROP_LITERAL(opcode) \ 272 ((opcode) >= CBC_PUSH_PROP_LITERAL && (opcode) <= CBC_PUSH_PROP_THIS_LITERAL) 273 274 #define PARSER_PUSH_LITERAL_TO_PUSH_PROP_LITERAL(opcode) \ 275 (uint16_t) ((opcode) + (CBC_PUSH_PROP_LITERAL - CBC_PUSH_LITERAL)) 276 277 #define PARSER_PUSH_PROP_LITERAL_TO_PUSH_LITERAL(opcode) \ 278 (uint16_t) ((opcode) - (CBC_PUSH_PROP_LITERAL - CBC_PUSH_LITERAL)) 279 280 #define PARSER_PUSH_PROP_TO_PUSH_PROP_REFERENCE(opcode) \ 281 (uint16_t) ((opcode) + (CBC_PUSH_PROP_REFERENCE - CBC_PUSH_PROP)) 282 283 #define PARSER_PUSH_PROP_REFERENCE_TO_PUSH_PROP(opcode) \ 284 (uint16_t) ((opcode) - (CBC_PUSH_PROP_REFERENCE - CBC_PUSH_PROP)) 285 286 #define PARSER_GET_LITERAL(literal_index) \ 287 ((lexer_literal_t *) parser_list_get (&context_p->literal_pool, (literal_index))) 288 289 #define PARSER_TO_BINARY_OPERATION_WITH_RESULT(opcode) \ 290 (PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_PUSH_RESULT) 291 292 #define PARSER_TO_BINARY_OPERATION_WITH_BLOCK(opcode) \ 293 ((uint16_t) (PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_BLOCK)) 294 295 #define PARSER_GET_FLAGS(op) \ 296 (PARSER_IS_BASIC_OPCODE (op) ? cbc_flags[(op)] : cbc_ext_flags[PARSER_GET_EXT_OPCODE (op)]) 297 298 #define PARSER_OPCODE_IS_RETURN(op) \ 299 ((op) == CBC_RETURN || (op) == CBC_RETURN_WITH_BLOCK || (op) == CBC_RETURN_WITH_LITERAL) 300 301 #define PARSER_ARGS_EQ(op, types) \ 302 ((PARSER_GET_FLAGS (op) & CBC_ARG_TYPES) == (types)) 303 304 /** 305 * All data allocated by the parser is 306 * stored in parser_data_pages in the memory. 307 */ 308 typedef struct parser_mem_page_t 309 { 310 struct parser_mem_page_t *next_p; /**< next page */ 311 uint8_t bytes[1]; /**< memory bytes */ 312 } parser_mem_page_t; 313 314 /** 315 * Structure for managing parser memory. 316 */ 317 typedef struct 318 { 319 parser_mem_page_t *first_p; /**< first allocated page */ 320 parser_mem_page_t *last_p; /**< last allocated page */ 321 uint32_t last_position; /**< position of the last allocated byte */ 322 } parser_mem_data_t; 323 324 /** 325 * Parser memory list. 326 */ 327 typedef struct 328 { 329 parser_mem_data_t data; /**< storage space */ 330 uint32_t page_size; /**< size of each page */ 331 uint32_t item_size; /**< size of each item */ 332 uint32_t item_count; /**< number of items on each page */ 333 } parser_list_t; 334 335 /** 336 * Iterator for parser memory list. 337 */ 338 typedef struct 339 { 340 parser_list_t *list_p; /**< parser list */ 341 parser_mem_page_t *current_p; /**< currently processed page */ 342 size_t current_position; /**< current position on the page */ 343 } parser_list_iterator_t; 344 345 /** 346 * Parser memory stack. 347 */ 348 typedef struct 349 { 350 parser_mem_data_t data; /**< storage space */ 351 parser_mem_page_t *free_page_p; /**< space for fast allocation */ 352 } parser_stack_t; 353 354 /** 355 * Iterator for parser memory stack. 356 */ 357 typedef struct 358 { 359 parser_mem_page_t *current_p; /**< currently processed page */ 360 size_t current_position; /**< current position on the page */ 361 } parser_stack_iterator_t; 362 363 /** 364 * Branch type. 365 */ 366 typedef struct 367 { 368 parser_mem_page_t *page_p; /**< branch location page */ 369 uint32_t offset; /**< branch location offset */ 370 } parser_branch_t; 371 372 /** 373 * Branch chain type. 374 */ 375 typedef struct parser_branch_node_t 376 { 377 struct parser_branch_node_t *next_p; /**< next linked list node */ 378 parser_branch_t branch; /**< branch */ 379 } parser_branch_node_t; 380 381 /** 382 * Items of scope stack. 383 */ 384 typedef struct 385 { 386 uint16_t map_from; /**< original literal index */ 387 uint16_t map_to; /**< encoded register or literal index and flags */ 388 } parser_scope_stack_t; 389 390 /** 391 * This item represents a function literal in the scope stack. 392 * 393 * When map_from == PARSER_SCOPE_STACK_FUNC: 394 * map_to represents the literal reserved for a function literal 395 * Note: the name of the function is the previous value in the scope stack 396 * Note: map_to is not encoded in this case 397 */ 398 #define PARSER_SCOPE_STACK_FUNC 0xffff 399 400 #if ENABLED (JERRY_ES2015) 401 402 /** 403 * Mask for decoding the register index of map_to 404 */ 405 #define PARSER_SCOPE_STACK_REGISTER_MASK 0x3fff 406 407 /** 408 * Function statements with the name specified 409 * in map_from should not be copied to global scope. 410 */ 411 #define PARSER_SCOPE_STACK_NO_FUNCTION_COPY 0x8000 412 413 /** 414 * The scope stack item represents a const binding stored in register 415 */ 416 #define PARSER_SCOPE_STACK_IS_CONST_REG 0x4000 417 418 /** 419 * The scope stack item represents a binding which has already created with ECMA_VALUE_UNINITIALIZED 420 */ 421 #define PARSER_SCOPE_STACK_IS_LOCAL_CREATED (PARSER_SCOPE_STACK_IS_CONST_REG) 422 423 #endif /* ENABLED (JERRY_ES2015) */ 424 425 /** 426 * Starting literal index for registers. 427 */ 428 #define PARSER_REGISTER_START 0x8000 429 430 /* Forward definitions for js-scanner-internal.h. */ 431 struct scanner_context_t; 432 typedef struct scanner_context_t scanner_context_t; 433 434 #if ENABLED (JERRY_DEBUGGER) 435 /** 436 * Extra information for each breakpoint. 437 */ 438 typedef struct 439 { 440 uint32_t value; /**< line or offset of the breakpoint */ 441 } parser_breakpoint_info_t; 442 443 /** 444 * Maximum number of breakpoint info. 445 */ 446 #define PARSER_MAX_BREAKPOINT_INFO_COUNT \ 447 (JERRY_DEBUGGER_TRANSPORT_MAX_BUFFER_SIZE / sizeof (parser_breakpoint_info_t)) 448 449 #endif /* ENABLED (JERRY_DEBUGGER) */ 450 451 /** 452 * Those members of a context which needs 453 * to be saved when a sub-function is parsed. 454 */ 455 typedef struct parser_saved_context_t 456 { 457 /* Parser members. */ 458 uint32_t status_flags; /**< parsing options */ 459 uint16_t stack_depth; /**< current stack depth */ 460 uint16_t stack_limit; /**< maximum stack depth */ 461 struct parser_saved_context_t *prev_context_p; /**< last saved context */ 462 parser_stack_iterator_t last_statement; /**< last statement position */ 463 464 /* Literal types */ 465 uint16_t argument_count; /**< number of function arguments */ 466 uint16_t register_count; /**< number of registers */ 467 uint16_t literal_count; /**< number of literals */ 468 469 /* Memory storage members. */ 470 parser_mem_data_t byte_code; /**< byte code buffer */ 471 uint32_t byte_code_size; /**< byte code size for branches */ 472 parser_mem_data_t literal_pool_data; /**< literal list */ 473 parser_scope_stack_t *scope_stack_p; /**< scope stack */ 474 uint16_t scope_stack_size; /**< size of scope stack */ 475 uint16_t scope_stack_top; /**< preserved top of scope stack */ 476 uint16_t scope_stack_reg_top; /**< preserved top register of scope stack */ 477 #if ENABLED (JERRY_ES2015) 478 uint16_t scope_stack_global_end; /**< end of global declarations of a function */ 479 ecma_value_t tagged_template_literal_cp; /**< compessed pointer to the tagged template literal collection */ 480 #endif /* ENABLED (JERRY_ES2015) */ 481 482 #ifndef JERRY_NDEBUG 483 uint16_t context_stack_depth; /**< current context stack depth */ 484 #endif /* !JERRY_NDEBUG */ 485 } parser_saved_context_t; 486 487 /** 488 * Shared parser context. 489 */ 490 typedef struct 491 { 492 PARSER_TRY_CONTEXT (try_buffer); /**< try_buffer */ 493 parser_error_t error; /**< error code */ 494 /** Union for rarely used members. */ 495 union 496 { 497 void *allocated_buffer_p; /**< dinamically allocated buffer 498 * which needs to be freed on error */ 499 scanner_context_t *scanner_context_p; /**< scanner context for the pre-scanner */ 500 } u; 501 uint32_t allocated_buffer_size; /**< size of the dinamically allocated buffer */ 502 503 /* Parser members. */ 504 uint32_t status_flags; /**< status flags */ 505 uint32_t global_status_flags; /**< global status flags */ 506 uint16_t stack_depth; /**< current stack depth */ 507 uint16_t stack_limit; /**< maximum stack depth */ 508 parser_saved_context_t *last_context_p; /**< last saved context */ 509 parser_stack_iterator_t last_statement; /**< last statement position */ 510 511 #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) 512 ecma_module_node_t *module_current_node_p; /**< import / export node that is being processed */ 513 lexer_literal_t *module_identifier_lit_p; /**< the literal for the identifier of the current element */ 514 #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ 515 516 /* Lexer members. */ 517 lexer_token_t token; /**< current token */ 518 lexer_lit_object_t lit_object; /**< current literal object */ 519 const uint8_t *source_p; /**< next source byte */ 520 const uint8_t *source_end_p; /**< last source byte */ 521 parser_line_counter_t line; /**< current line */ 522 parser_line_counter_t column; /**< current column */ 523 524 /* Scanner members. */ 525 scanner_info_t *next_scanner_info_p; /**< next scanner info block */ 526 scanner_info_t *active_scanner_info_p; /**< currently active scanner info block */ 527 scanner_info_t *skipped_scanner_info_p; /**< next scanner info block */ 528 scanner_info_t *skipped_scanner_info_end_p; /**< currently active scanner info block */ 529 530 /* Compact byte code members. */ 531 cbc_argument_t last_cbc; /**< argument of the last cbc */ 532 uint16_t last_cbc_opcode; /**< opcode of the last cbc */ 533 534 /* Literal types */ 535 uint16_t argument_count; /**< number of function arguments */ 536 uint16_t register_count; /**< number of registers */ 537 uint16_t literal_count; /**< number of literals */ 538 539 /* Memory storage members. */ 540 parser_mem_data_t byte_code; /**< byte code buffer */ 541 uint32_t byte_code_size; /**< current byte code size for branches */ 542 parser_list_t literal_pool; /**< literal list */ 543 parser_mem_data_t stack; /**< storage space */ 544 parser_scope_stack_t *scope_stack_p; /**< scope stack */ 545 parser_mem_page_t *free_page_p; /**< space for fast allocation */ 546 uint16_t scope_stack_size; /**< size of scope stack */ 547 uint16_t scope_stack_top; /**< current top of scope stack */ 548 uint16_t scope_stack_reg_top; /**< current top register of scope stack */ 549 #if ENABLED (JERRY_ES2015) 550 uint16_t scope_stack_global_end; /**< end of global declarations of a function */ 551 ecma_value_t tagged_template_literal_cp; /**< compessed pointer to the tagged template literal collection */ 552 #endif /* ENABLED (JERRY_ES2015) */ 553 uint8_t stack_top_uint8; /**< top byte stored on the stack */ 554 555 #ifndef JERRY_NDEBUG 556 /* Variables for debugging / logging. */ 557 uint16_t context_stack_depth; /**< current context stack depth */ 558 #endif /* !JERRY_NDEBUG */ 559 560 #if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) 561 int is_show_opcodes; /**< show opcodes */ 562 uint32_t total_byte_code_size; /**< total byte code size */ 563 #endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */ 564 565 #if ENABLED (JERRY_DEBUGGER) 566 parser_breakpoint_info_t breakpoint_info[PARSER_MAX_BREAKPOINT_INFO_COUNT]; /**< breakpoint info list */ 567 uint16_t breakpoint_info_count; /**< current breakpoint index */ 568 parser_line_counter_t last_breakpoint_line; /**< last line where breakpoint has been inserted */ 569 #endif /* ENABLED (JERRY_DEBUGGER) */ 570 571 #if ENABLED (JERRY_LINE_INFO) 572 parser_line_counter_t last_line_info_line; /**< last line where line info has been inserted */ 573 #endif /* ENABLED (JERRY_LINE_INFO) */ 574 } parser_context_t; 575 576 /** 577 * @} 578 * @} 579 * @} 580 * 581 * \addtogroup mem Memory allocation 582 * @{ 583 * 584 * \addtogroup mem_parser Parser memory manager 585 * @{ 586 */ 587 588 /* Memory management. 589 * Note: throws an error if unsuccessful. */ 590 void *parser_malloc (parser_context_t *context_p, size_t size); 591 void parser_free (void *ptr, size_t size); 592 void *parser_malloc_local (parser_context_t *context_p, size_t size); 593 void parser_free_local (void *ptr, size_t size); 594 void parser_free_allocated_buffer (parser_context_t *context_p); 595 596 /* Parser byte stream. */ 597 598 void parser_cbc_stream_init (parser_mem_data_t *data_p); 599 void parser_cbc_stream_free (parser_mem_data_t *data_p); 600 void parser_cbc_stream_alloc_page (parser_context_t *context_p, parser_mem_data_t *data_p); 601 602 /* Parser list. Ensures pointer alignment. */ 603 604 void parser_list_init (parser_list_t *list_p, uint32_t item_size, uint32_t item_count); 605 void parser_list_free (parser_list_t *list_p); 606 void parser_list_reset (parser_list_t *list_p); 607 void *parser_list_append (parser_context_t *context_p, parser_list_t *list_p); 608 void *parser_list_get (parser_list_t *list_p, size_t index); 609 void parser_list_iterator_init (parser_list_t *list_p, parser_list_iterator_t *iterator_p); 610 void *parser_list_iterator_next (parser_list_iterator_t *iterator_p); 611 612 /* Parser stack. Optimized for pushing bytes. 613 * Pop functions never throws error. */ 614 615 void parser_stack_init (parser_context_t *context_p); 616 void parser_stack_free (parser_context_t *context_p); 617 void parser_stack_push_uint8 (parser_context_t *context_p, uint8_t uint8_value); 618 void parser_stack_pop_uint8 (parser_context_t *context_p); 619 void parser_stack_push_uint16 (parser_context_t *context_p, uint16_t uint16_value); 620 uint16_t parser_stack_pop_uint16 (parser_context_t *context_p); 621 void parser_stack_push (parser_context_t *context_p, const void *data_p, uint32_t length); 622 void parser_stack_pop (parser_context_t *context_p, void *data_p, uint32_t length); 623 void parser_stack_iterator_skip (parser_stack_iterator_t *iterator, size_t length); 624 void parser_stack_iterator_read (parser_stack_iterator_t *iterator, void *data_p, size_t length); 625 void parser_stack_iterator_write (parser_stack_iterator_t *iterator, const void *data_p, size_t length); 626 627 /** 628 * @} 629 * @} 630 * 631 * \addtogroup parser Parser 632 * @{ 633 * 634 * \addtogroup jsparser JavaScript 635 * @{ 636 * 637 * \addtogroup jsparser_utils Utility 638 * @{ 639 */ 640 641 /* Compact byte code emitting functions. */ 642 643 void parser_flush_cbc (parser_context_t *context_p); 644 void parser_emit_cbc (parser_context_t *context_p, uint16_t opcode); 645 void parser_emit_cbc_literal (parser_context_t *context_p, uint16_t opcode, uint16_t literal_index); 646 void parser_emit_cbc_literal_value (parser_context_t *context_p, uint16_t opcode, uint16_t literal_index, 647 uint16_t value); 648 void parser_emit_cbc_literal_from_token (parser_context_t *context_p, uint16_t opcode); 649 void parser_emit_cbc_call (parser_context_t *context_p, uint16_t opcode, size_t call_arguments); 650 void parser_emit_cbc_push_number (parser_context_t *context_p, bool is_negative_number); 651 void parser_emit_cbc_forward_branch (parser_context_t *context_p, uint16_t opcode, parser_branch_t *branch_p); 652 parser_branch_node_t *parser_emit_cbc_forward_branch_item (parser_context_t *context_p, uint16_t opcode, 653 parser_branch_node_t *next_p); 654 void parser_emit_cbc_backward_branch (parser_context_t *context_p, uint16_t opcode, uint32_t offset); 655 void parser_set_branch_to_current_position (parser_context_t *context_p, parser_branch_t *branch_p); 656 void parser_set_breaks_to_current_position (parser_context_t *context_p, parser_branch_node_t *current_p); 657 void parser_set_continues_to_current_position (parser_context_t *context_p, parser_branch_node_t *current_p); 658 659 /* Convenience macros. */ 660 #define parser_emit_cbc_ext(context_p, opcode) \ 661 parser_emit_cbc ((context_p), PARSER_TO_EXT_OPCODE (opcode)) 662 #define parser_emit_cbc_ext_literal(context_p, opcode, literal_index) \ 663 parser_emit_cbc_literal ((context_p), PARSER_TO_EXT_OPCODE (opcode), (literal_index)) 664 #define parser_emit_cbc_ext_literal_from_token(context_p, opcode) \ 665 parser_emit_cbc_literal_from_token ((context_p), PARSER_TO_EXT_OPCODE (opcode)) 666 #define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \ 667 parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments)) 668 #define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \ 669 parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments)) 670 #define parser_emit_cbc_ext_forward_branch(context_p, opcode, branch_p) \ 671 parser_emit_cbc_forward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (branch_p)) 672 #define parser_emit_cbc_ext_backward_branch(context_p, opcode, offset) \ 673 parser_emit_cbc_backward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (offset)) 674 675 /** 676 * @} 677 * 678 * \addtogroup jsparser_lexer Lexer 679 * @{ 680 */ 681 682 /* Lexer functions */ 683 684 void lexer_next_token (parser_context_t *context_p); 685 bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t character); 686 bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1, 687 lit_utf8_byte_t character2); 688 uint8_t lexer_consume_next_character (parser_context_t *context_p); 689 bool lexer_check_post_primary_exp (parser_context_t *context_p); 690 #if ENABLED (JERRY_ES2015) 691 void lexer_skip_empty_statements (parser_context_t *context_p); 692 bool lexer_check_arrow (parser_context_t *context_p); 693 bool lexer_check_arrow_param (parser_context_t *context_p); 694 bool lexer_check_yield_no_arg (parser_context_t *context_p); 695 bool lexer_consume_generator (parser_context_t *context_p); 696 void lexer_update_await_yield (parser_context_t *context_p, uint32_t status_flags); 697 #endif /* ENABLED (JERRY_ES2015) */ 698 void lexer_parse_string (parser_context_t *context_p, lexer_string_options_t opts); 699 void lexer_expect_identifier (parser_context_t *context_p, uint8_t literal_type); 700 bool lexer_scan_identifier (parser_context_t *context_p); 701 void lexer_check_property_modifier (parser_context_t *context_p); 702 void lexer_convert_ident_to_cesu8 (uint8_t *destination_p, const uint8_t *source_p, prop_length_t length); 703 704 const uint8_t *lexer_convert_literal_to_chars (parser_context_t *context_p, const lexer_lit_location_t *literal_p, 705 uint8_t *local_byte_array_p, lexer_string_options_t opts); 706 void lexer_expect_object_literal_id (parser_context_t *context_p, uint32_t ident_opts); 707 void lexer_construct_literal_object (parser_context_t *context_p, const lexer_lit_location_t *lit_location_p, 708 uint8_t literal_type); 709 bool lexer_construct_number_object (parser_context_t *context_p, bool is_expr, bool is_negative_number); 710 void lexer_convert_push_number_to_push_literal (parser_context_t *context_p); 711 uint16_t lexer_construct_function_object (parser_context_t *context_p, uint32_t extra_status_flags); 712 void lexer_construct_regexp_object (parser_context_t *context_p, bool parse_only); 713 bool lexer_compare_identifier_to_string (const lexer_lit_location_t *left_p, const uint8_t *right_p, size_t size); 714 bool lexer_compare_identifiers (parser_context_t *context_p, const lexer_lit_location_t *left_p, 715 const lexer_lit_location_t *right_p); 716 bool lexer_current_is_literal (parser_context_t *context_p, const lexer_lit_location_t *right_ident_p); 717 bool lexer_string_is_use_strict (parser_context_t *context_p); 718 bool lexer_string_is_directive (parser_context_t *context_p); 719 #if ENABLED (JERRY_ES2015) 720 bool lexer_token_is_identifier (parser_context_t *context_p, const char *identifier_p, 721 size_t identifier_length); 722 bool lexer_token_is_let (parser_context_t *context_p); 723 bool lexer_token_is_async (parser_context_t *context_p); 724 #endif /* ENABLED (JERRY_ES2015) */ 725 bool lexer_compare_literal_to_string (parser_context_t *context_p, const char *string_p, size_t string_length); 726 uint8_t lexer_convert_binary_lvalue_token_to_binary (uint8_t token); 727 728 /** 729 * @} 730 * 731 * \addtogroup jsparser_expr Expression parser 732 * @{ 733 */ 734 735 /* Parser functions. */ 736 737 void parser_parse_block_expression (parser_context_t *context_p, int options); 738 void parser_parse_expression_statement (parser_context_t *context_p, int options); 739 void parser_parse_expression (parser_context_t *context_p, int options); 740 #if ENABLED (JERRY_ES2015) 741 void parser_parse_class (parser_context_t *context_p, bool is_statement); 742 void parser_parse_initializer (parser_context_t *context_p, parser_pattern_flags_t flags); 743 void parser_parse_initializer_by_next_char (parser_context_t *context_p, parser_pattern_flags_t flags); 744 #endif /* ENABLED (JERRY_ES2015) */ 745 746 /** 747 * @} 748 * 749 * \addtogroup jsparser_scanner Scanner 750 * @{ 751 */ 752 753 void scanner_release_next (parser_context_t *context_p, size_t size); 754 void scanner_set_active (parser_context_t *context_p); 755 void scanner_revert_active (parser_context_t *context_p); 756 void scanner_release_active (parser_context_t *context_p, size_t size); 757 void scanner_release_switch_cases (scanner_case_info_t *case_p); 758 void scanner_seek (parser_context_t *context_p); 759 void scanner_reverse_info_list (parser_context_t *context_p); 760 void scanner_cleanup (parser_context_t *context_p); 761 762 bool scanner_is_context_needed (parser_context_t *context_p, parser_check_context_type_t check_type); 763 #if ENABLED (JERRY_ES2015) 764 bool scanner_scope_find_let_declaration (parser_context_t *context_p, lexer_lit_location_t *literal_p); 765 bool scanner_try_scan_new_target (parser_context_t *context_p); 766 void scanner_check_variables (parser_context_t *context_p); 767 #endif /* ENABLED (JERRY_ES2015) */ 768 void scanner_create_variables (parser_context_t *context_p, uint32_t option_flags); 769 770 void scanner_get_location (scanner_location_t *location_p, parser_context_t *context_p); 771 void scanner_set_location (parser_context_t *context_p, scanner_location_t *location_p); 772 uint16_t scanner_decode_map_to (parser_scope_stack_t *stack_item_p); 773 #if ENABLED (JERRY_ES2015) 774 bool scanner_literal_is_const_reg (parser_context_t *context_p, uint16_t literal_index); 775 bool scanner_literal_is_created (parser_context_t *context_p, uint16_t literal_index); 776 #endif /* ENABLED (JERRY_ES2015) */ 777 778 void scanner_scan_all (parser_context_t *context_p, const uint8_t *arg_list_p, const uint8_t *arg_list_end_p, 779 const uint8_t *source_p, const uint8_t *source_end_p); 780 781 /** 782 * @} 783 * 784 * \addtogroup jsparser_stmt Statement parser 785 * @{ 786 */ 787 788 void parser_parse_statements (parser_context_t *context_p); 789 void parser_free_jumps (parser_stack_iterator_t iterator); 790 791 #if ENABLED (JERRY_ES2015_MODULE_SYSTEM) 792 /** 793 * @} 794 * 795 * \addtogroup jsparser_stmt Module statement parser 796 * @{ 797 */ 798 799 extern const lexer_lit_location_t lexer_default_literal; 800 void parser_module_add_export_node_to_context (parser_context_t *context_p); 801 void parser_module_add_import_node_to_context (parser_context_t *context_p); 802 void parser_module_check_request_place (parser_context_t *context_p); 803 void parser_module_context_init (void); 804 void parser_module_handle_module_specifier (parser_context_t *context_p); 805 void parser_module_handle_requests (parser_context_t *context_p); 806 void parser_module_parse_export_clause (parser_context_t *context_p); 807 void parser_module_parse_import_clause (parser_context_t *context_p); 808 void parser_module_set_default (parser_context_t *context_p); 809 ecma_module_node_t *parser_module_create_module_node (parser_context_t *context_p); 810 bool parser_module_check_duplicate_import (parser_context_t *context_p, ecma_string_t *local_name_p); 811 bool parser_module_check_duplicate_export (parser_context_t *context_p, ecma_string_t *export_name_p); 812 void parser_module_append_export_name (parser_context_t *context_p); 813 void parser_module_add_names_to_node (parser_context_t *context_p, 814 ecma_string_t *imex_name_p, 815 ecma_string_t *local_name_p); 816 817 #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ 818 819 /** 820 * @} 821 * 822 * \addtogroup jsparser_parser Parser 823 * @{ 824 */ 825 826 ecma_compiled_code_t *parser_parse_function (parser_context_t *context_p, uint32_t status_flags); 827 #if ENABLED (JERRY_ES2015) 828 ecma_compiled_code_t *parser_parse_arrow_function (parser_context_t *context_p, uint32_t status_flags); 829 #endif /* ENABLED (JERRY_ES2015) */ 830 831 /* Error management. */ 832 833 void parser_raise_error (parser_context_t *context_p, parser_error_t error); 834 835 /* Debug functions. */ 836 837 #if ENABLED (JERRY_DEBUGGER) 838 839 void parser_append_breakpoint_info (parser_context_t *context_p, jerry_debugger_header_type_t type, uint32_t value); 840 841 #endif /* ENABLED (JERRY_DEBUGGER) */ 842 843 #if ENABLED (JERRY_LINE_INFO) 844 845 void parser_emit_line_info (parser_context_t *context_p, uint32_t line, bool flush_cbc); 846 847 #endif /* ENABLED (JERRY_LINE_INFO) */ 848 849 /** 850 * @} 851 * @} 852 * @} 853 */ 854 855 #endif /* !JS_PARSER_INTERNAL_H */ 856