• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/statement.h>
20 
21 namespace panda::es2panda::compiler {
22 
ScopeContext(PandaGen * pg,binder::Scope * newScope)23 ScopeContext::ScopeContext(PandaGen *pg, binder::Scope *newScope) : pg_(pg), prevScope_(pg_->scope_)
24 {
25     pg_->scope_ = newScope;
26 }
27 
~ScopeContext()28 ScopeContext::~ScopeContext()
29 {
30     pg_->scope_ = prevScope_;
31 }
32 
Initialize(PandaGen * pg)33 void EnvScope::Initialize(PandaGen *pg)
34 {
35     pg_ = pg;
36     prev_ = pg_->envScope_;
37     pg_->envScope_ = this;
38 }
39 
~EnvScope()40 EnvScope::~EnvScope()
41 {
42     if (!pg_) {
43         return;
44     }
45 
46     pg_->envScope_ = prev_;
47 }
48 
InitLoopContext(PandaGen * pg,binder::VariableScope * scope)49 void LoopEnvScope::InitLoopContext(PandaGen *pg, binder::VariableScope *scope)
50 {
51     if (!HasEnv()) {
52         return;
53     }
54 
55     Initialize(pg);
56     ASSERT(scope->NeedLexEnv());
57     pg_->NewLexicalEnv(scope_->Node(), scope->LexicalSlots(), scope_);
58 }
59 
CopyPerIterationCtx()60 void LoopEnvScope::CopyPerIterationCtx()
61 {
62     if (!HasEnv()) {
63         return;
64     }
65 
66     auto num = scope_->LexicalSlots();
67     RegScope rs(pg_);
68     std::vector<VReg> lexicals;
69     lexicals.reserve(num);
70     for (uint32_t i = 0; i < num; i++) {
71         VReg lexical = pg_->AllocReg();
72         pg_->LoadLexicalVar(scope_->Node(), 0, i);
73         pg_->StoreAccumulator(scope_->Node(), lexical);
74         lexicals.push_back(lexical);
75     }
76     pg_->PopLexEnv(scope_->Node());
77     pg_->NewLexicalEnv(scope_->Node(), num, scope_);
78 
79     for (uint32_t i = 0; i < num; i++) {
80         pg_->StoreLexicalVar(scope_->Node(), 0, i, lexicals[i]);
81     }
82 }
83 
PopLexEnvBeforeTheNextIter()84 void LoopEnvScope::PopLexEnvBeforeTheNextIter()
85 {
86     if (!HasEnv()) {
87         return;
88     }
89 
90     pg_->PopLexEnv(scope_->Node());
91 }
92 
93 }  // namespace panda::es2panda::compiler
94