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 #include "frontend/OptionalEmitter.h"
8 
9 #include "frontend/BytecodeEmitter.h"
10 #include "frontend/IfEmitter.h"  // IfEmitter, InternalIfEmitter, CondEmitter
11 #include "frontend/SharedContext.h"
12 #include "vm/Opcodes.h"
13 #include "vm/StringType.h"
14 
15 using namespace js;
16 using namespace js::frontend;
17 
OptionalEmitter(BytecodeEmitter * bce,int32_t initialDepth)18 OptionalEmitter::OptionalEmitter(BytecodeEmitter* bce, int32_t initialDepth)
19     : bce_(bce), tdzCache_(bce), initialDepth_(initialDepth) {}
20 
emitJumpShortCircuit()21 bool OptionalEmitter::emitJumpShortCircuit() {
22   MOZ_ASSERT(state_ == State::Start || state_ == State::ShortCircuit ||
23              state_ == State::ShortCircuitForCall);
24   MOZ_ASSERT(initialDepth_ + 1 == bce_->bytecodeSection().stackDepth());
25   InternalIfEmitter ifEmitter(bce_);
26   if (!bce_->emitPushNotUndefinedOrNull()) {
27     //              [stack] OBJ NOT-UNDEFINED-OR-NULL
28     return false;
29   }
30 
31   if (!bce_->emit1(JSOp::Not)) {
32     //              [stack] OBJ UNDEFINED-OR-NULL
33     return false;
34   }
35 
36   if (!ifEmitter.emitThen()) {
37     return false;
38   }
39 
40   if (!bce_->emitJump(JSOp::Goto, &jumpShortCircuit_)) {
41     //              [stack] UNDEFINED-OR-NULL
42     return false;
43   }
44 
45   if (!ifEmitter.emitEnd()) {
46     return false;
47   }
48 #ifdef DEBUG
49   state_ = State::ShortCircuit;
50 #endif
51   return true;
52 }
53 
emitJumpShortCircuitForCall()54 bool OptionalEmitter::emitJumpShortCircuitForCall() {
55   MOZ_ASSERT(state_ == State::Start || state_ == State::ShortCircuit ||
56              state_ == State::ShortCircuitForCall);
57   int32_t depth = bce_->bytecodeSection().stackDepth();
58   MOZ_ASSERT(initialDepth_ + 2 == depth);
59   if (!bce_->emit1(JSOp::Swap)) {
60     //              [stack] THIS CALLEE
61     return false;
62   }
63 
64   InternalIfEmitter ifEmitter(bce_);
65   if (!bce_->emitPushNotUndefinedOrNull()) {
66     //              [stack] THIS CALLEE NOT-UNDEFINED-OR-NULL
67     return false;
68   }
69 
70   if (!bce_->emit1(JSOp::Not)) {
71     //              [stack] THIS CALLEE UNDEFINED-OR-NULL
72     return false;
73   }
74 
75   if (!ifEmitter.emitThen()) {
76     return false;
77   }
78 
79   if (!bce_->emit1(JSOp::Pop)) {
80     //              [stack] THIS
81     return false;
82   }
83 
84   if (!bce_->emitJump(JSOp::Goto, &jumpShortCircuit_)) {
85     //              [stack] UNDEFINED-OR-NULL
86     return false;
87   }
88 
89   if (!ifEmitter.emitEnd()) {
90     return false;
91   }
92 
93   bce_->bytecodeSection().setStackDepth(depth);
94 
95   if (!bce_->emit1(JSOp::Swap)) {
96     //              [stack] THIS CALLEE
97     return false;
98   }
99 #ifdef DEBUG
100   state_ = State::ShortCircuitForCall;
101 #endif
102   return true;
103 }
104 
emitOptionalJumpTarget(JSOp op,Kind kind)105 bool OptionalEmitter::emitOptionalJumpTarget(JSOp op,
106                                              Kind kind /* = Kind::Other */) {
107 #ifdef DEBUG
108   int32_t depth = bce_->bytecodeSection().stackDepth();
109 #endif
110   MOZ_ASSERT(state_ == State::ShortCircuit ||
111              state_ == State::ShortCircuitForCall);
112 
113   // if we get to this point, it means that the optional chain did not short
114   // circuit, so we should skip the short circuiting bytecode.
115   if (!bce_->emitJump(JSOp::Goto, &jumpFinish_)) {
116     //              [stack] # if call
117     //              [stack] CALLEE THIS
118     //              [stack] # otherwise, if defined
119     //              [stack] VAL
120     //              [stack] # otherwise
121     //              [stack] UNDEFINED-OR-NULL
122     return false;
123   }
124 
125   if (!bce_->emitJumpTargetAndPatch(jumpShortCircuit_)) {
126     //              [stack] UNDEFINED-OR-NULL
127     return false;
128   }
129 
130   // reset stack depth to the depth when we jumped
131   bce_->bytecodeSection().setStackDepth(initialDepth_ + 1);
132 
133   if (!bce_->emit1(JSOp::Pop)) {
134     //              [stack]
135     return false;
136   }
137 
138   if (!bce_->emit1(op)) {
139     //              [stack] JSOP
140     return false;
141   }
142 
143   if (kind == Kind::Reference) {
144     if (!bce_->emit1(op)) {
145       //              [stack] JSOP JSOP
146       return false;
147     }
148   }
149 
150   MOZ_ASSERT(depth == bce_->bytecodeSection().stackDepth());
151 
152   if (!bce_->emitJumpTargetAndPatch(jumpFinish_)) {
153     //              [stack] # if call
154     //              [stack] CALLEE THIS
155     //              [stack] # otherwise
156     //              [stack] VAL
157     return false;
158   }
159 #ifdef DEBUG
160   state_ = State::JumpEnd;
161 #endif
162   return true;
163 }
164