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