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"
15 #include "frontend/BytecodeControlStructures.h"
16 #include "frontend/TDZCheckCache.h"
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,
37 class MOZ_STACK_CLASS WhileEmitter
{
38 #ifdef ENABLE_DECORATORS
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_
;
50 // The state of this emitter.
52 // +-------+ emitCond +------+ emitBody +------+ emitEnd +-----+
53 // | Start |--------->| Cond |--------->| Body |--------->| End |
54 // +-------+ +------+ +------+ +-----+
59 // After calling emitCond.
62 // After calling emitBody.
65 // After calling emitEnd.
68 State state_
= State::Start
;
72 explicit WhileEmitter(BytecodeEmitter
* bce
);
74 // Parameters are the offset in the source code for each character below:
76 // while ( x < 20 ) { ... }
84 [[nodiscard
]] bool emitCond(uint32_t whilePos
, uint32_t condPos
,
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
{
95 explicit InternalWhileEmitter(BytecodeEmitter
* bce
) : WhileEmitter(bce
) {}
96 [[nodiscard
]] bool emitCond();
100 } /* namespace frontend */
103 #endif /* frontend_WhileEmitter_h */