Bug 1865597 - Add error checking when initializing parallel marking and disable on...
[gecko.git] / js / src / frontend / WhileEmitter.h
blob7c21dc313b3e8f9e6a1972491a980b80306c3367
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef frontend_WhileEmitter_h
8 #define frontend_WhileEmitter_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
13 #include <stdint.h>
15 #include "frontend/BytecodeControlStructures.h"
16 #include "frontend/TDZCheckCache.h"
18 namespace js {
19 namespace frontend {
21 struct BytecodeEmitter;
23 // Class for emitting bytecode for while loop.
25 // Usage: (check for the return value is omitted for simplicity)
27 // `while (cond) body`
28 // WhileEmitter wh(this);
29 // wh.emitCond(offset_of_while,
30 // offset_of_body,
31 // offset_of_end);
32 // emit(cond);
33 // wh.emitBody();
34 // emit(body);
35 // wh.emitEnd();
37 class MOZ_STACK_CLASS WhileEmitter {
38 #ifdef ENABLE_DECORATORS
39 protected:
40 #endif
41 BytecodeEmitter* bce_;
43 mozilla::Maybe<LoopControl> loopInfo_;
45 // Cache for the loop body, which is enclosed by the cache in `loopInfo_`,
46 // which is effectively for the loop condition.
47 mozilla::Maybe<TDZCheckCache> tdzCacheForBody_;
49 #ifdef DEBUG
50 // The state of this emitter.
52 // +-------+ emitCond +------+ emitBody +------+ emitEnd +-----+
53 // | Start |--------->| Cond |--------->| Body |--------->| End |
54 // +-------+ +------+ +------+ +-----+
55 enum class State {
56 // The initial state.
57 Start,
59 // After calling emitCond.
60 Cond,
62 // After calling emitBody.
63 Body,
65 // After calling emitEnd.
66 End
68 State state_ = State::Start;
69 #endif
71 public:
72 explicit WhileEmitter(BytecodeEmitter* bce);
74 // Parameters are the offset in the source code for each character below:
76 // while ( x < 20 ) { ... }
77 // ^ ^ ^
78 // | | |
79 // | | endPos_
80 // | |
81 // | condPos_
82 // |
83 // whilePos_
84 [[nodiscard]] bool emitCond(uint32_t whilePos, uint32_t condPos,
85 uint32_t endPos);
86 [[nodiscard]] bool emitBody();
87 [[nodiscard]] bool emitEnd();
90 #ifdef ENABLE_DECORATORS
91 // This version is for emitting the condition in synthesized code that
92 // does not have a corresponding location in the source code.
93 class MOZ_STACK_CLASS InternalWhileEmitter : public WhileEmitter {
94 public:
95 explicit InternalWhileEmitter(BytecodeEmitter* bce) : WhileEmitter(bce) {}
96 [[nodiscard]] bool emitCond();
98 #endif
100 } /* namespace frontend */
101 } /* namespace js */
103 #endif /* frontend_WhileEmitter_h */