• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 - 2023 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/codeGen.h"
19 #include "compiler/core/pandagen.h"
20 #include "ir/statement.h"
21 
22 namespace panda::es2panda::compiler {
ScopeContext(CodeGen * cg,varbinder::Scope * newScope)23 ScopeContext::ScopeContext(CodeGen *cg, varbinder::Scope *newScope) : cg_(cg), prevScope_(cg_->scope_)
24 {
25     cg->scope_ = newScope;
26 }
27 
~ScopeContext()28 ScopeContext::~ScopeContext()
29 {
30     cg_->scope_ = prevScope_;
31 }
32 
Initialize(PandaGen * pg,VReg lexEnv)33 void EnvScope::Initialize(PandaGen *pg, VReg lexEnv)
34 {
35     pg_ = pg;
36     prev_ = pg_->envScope_;
37     lexEnv_ = lexEnv;
38     pg_->envScope_ = this;
39 }
40 
~EnvScope()41 EnvScope::~EnvScope()
42 {
43     if (pg_ == nullptr) {
44         return;
45     }
46 
47     pg_->envScope_ = prev_;
48 }
49 
CopyBindings(PandaGen * pg,varbinder::VariableScope * scope,varbinder::VariableFlags flag)50 void LoopEnvScope::CopyBindings(PandaGen *pg, varbinder::VariableScope *scope, varbinder::VariableFlags flag)
51 {
52     if (!HasEnv()) {
53         return;
54     }
55 
56     Initialize(pg, pg->AllocReg());
57 
58     pg_->NewLexEnv(scope_->Node(), scope->LexicalSlots());
59     pg_->StoreAccumulator(scope_->Node(), lexEnv_);
60 
61     ASSERT(scope->NeedLexEnv());
62 
63     for (const auto &[_, variable] : scope_->Bindings()) {
64         (void)_;
65         if (!variable->HasFlag(flag)) {
66             continue;
67         }
68 
69         pg->LoadLexicalVar(scope_->Node(), 1, variable->AsLocalVariable()->LexIdx());
70         pg->StoreLexicalVar(scope_->Parent()->Node(), 0, variable->AsLocalVariable()->LexIdx());
71     }
72 }
73 
CopyPetIterationCtx()74 void LoopEnvScope::CopyPetIterationCtx()
75 {
76     if (!HasEnv()) {
77         return;
78     }
79 
80     pg_->CopyLexEnv(scope_->Node());
81     pg_->StoreAccumulator(scope_->Node(), lexEnv_);
82 }
83 }  // namespace panda::es2panda::compiler
84