DoWhileEmitter.h (1966B)
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/. */ 6 7 #ifndef frontend_DoWhileEmitter_h 8 #define frontend_DoWhileEmitter_h 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/Maybe.h" 12 13 #include <stdint.h> 14 15 #include "frontend/BytecodeControlStructures.h" 16 17 namespace js { 18 namespace frontend { 19 20 struct BytecodeEmitter; 21 22 // Class for emitting bytecode for do-while loop. 23 // 24 // Usage: (check for the return value is omitted for simplicity) 25 // 26 // `do body while (cond);` 27 // DoWhileEmitter doWhile(this); 28 // doWhile.emitBody(offset_of_do, offset_of_body); 29 // emit(body); 30 // doWhile.emitCond(); 31 // emit(cond); 32 // doWhile.emitEnd(); 33 // 34 class MOZ_STACK_CLASS DoWhileEmitter { 35 BytecodeEmitter* bce_; 36 37 mozilla::Maybe<LoopControl> loopInfo_; 38 39 #ifdef DEBUG 40 // The state of this emitter. 41 // 42 // +-------+ emitBody +------+ emitCond +------+ emitEnd +-----+ 43 // | Start |--------->| Body |--------->| Cond |--------->| End | 44 // +-------+ +------+ +------+ +-----+ 45 enum class State { 46 // The initial state. 47 Start, 48 49 // After calling emitBody. 50 Body, 51 52 // After calling emitCond. 53 Cond, 54 55 // After calling emitEnd. 56 End 57 }; 58 State state_ = State::Start; 59 #endif 60 61 public: 62 explicit DoWhileEmitter(BytecodeEmitter* bce); 63 64 // Parameters are the offset in the source code for each character below: 65 // 66 // do { ... } while ( x < 20 ); 67 // ^ ^ 68 // | | 69 // | bodyPos 70 // | 71 // doPos 72 [[nodiscard]] bool emitBody(uint32_t doPos, uint32_t bodyPos); 73 [[nodiscard]] bool emitCond(); 74 [[nodiscard]] bool emitEnd(); 75 }; 76 77 } /* namespace frontend */ 78 } /* namespace js */ 79 80 #endif /* frontend_DoWhileEmitter_h */