1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 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#include "envScope.h" 17 18#include <compiler/core/pandagen.h> 19#include <ir/base/classDefinition.h> 20#include <ir/statement.h> 21 22namespace panda::es2panda::compiler { 23 24ScopeContext::ScopeContext(PandaGen *pg, binder::Scope *newScope) : pg_(pg), prevScope_(pg_->scope_) 25{ 26 pg_->scope_ = newScope; 27} 28 29ScopeContext::~ScopeContext() 30{ 31 pg_->scope_ = prevScope_; 32} 33 34void EnvScope::Initialize(PandaGen *pg) 35{ 36 pg_ = pg; 37 prev_ = pg_->envScope_; 38 pg_->envScope_ = this; 39} 40 41EnvScope::~EnvScope() 42{ 43 if (!pg_) { 44 return; 45 } 46 47 pg_->envScope_ = prev_; 48} 49 50bool VariableEnvScope::InitVariableContext(PandaGen *pg, binder::VariableScope *scope) 51{ 52 if (!scope->NeedLexEnv()) { 53 return false; 54 } 55 56 Initialize(pg); 57 pg_->NewLexicalEnv(scope->Node(), scope->LexicalSlots(), scope); 58 return true; 59} 60 61void LoopEnvScope::CopyPerIterationCtx() 62{ 63 if (!HasEnv()) { 64 return; 65 } 66 67 auto num = scope_->LexicalSlots(); 68 RegScope rs(pg_); 69 std::vector<VReg> lexicals; 70 lexicals.reserve(num); 71 for (uint32_t i = 0; i < num; i++) { 72 VReg lexical = pg_->AllocReg(); 73 pg_->LoadLexicalVar(scope_->Node(), 0, i); 74 pg_->StoreAccumulator(scope_->Node(), lexical); 75 lexicals.push_back(lexical); 76 } 77 pg_->PopLexEnv(scope_->Node()); 78 pg_->NewLexicalEnv(scope_->Node(), num, scope_); 79 80 for (uint32_t i = 0; i < num; i++) { 81 pg_->StoreLexicalVar(scope_->Node(), 0, i, lexicals[i]); 82 } 83} 84 85} // namespace panda::es2panda::compiler 86