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 "function.h"
17
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <binder/variable.h>
21 #include <compiler/base/lreference.h>
22 #include <compiler/core/pandagen.h>
23 #include <ir/base/classDefinition.h>
24 #include <ir/base/classProperty.h>
25 #include <ir/base/scriptFunction.h>
26 #include <ir/expressions/assignmentExpression.h>
27 #include <ir/expressions/identifier.h>
28 #include <ir/statements/blockStatement.h>
29 #include <ir/ts/tsParameterProperty.h>
30 #include <util/helpers.h>
31
32 namespace panda::es2panda::compiler {
33
CompileSourceBlock(PandaGen * pg,const ir::BlockStatement * block)34 static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block)
35 {
36 bool hasReturn = false;
37
38 const auto &statements = block->Statements();
39 pg->SetFirstStmt(statements.empty() ? block : statements.front());
40
41 for (const auto *stmt : statements) {
42 stmt->Compile(pg);
43
44 if (stmt->IsReturnStatement()) {
45 hasReturn = true;
46 }
47 }
48
49 if (hasReturn) {
50 return;
51 }
52
53 pg->ImplicitReturn(statements.empty() ? block : statements.back());
54 }
55
CompileFunctionParameterDeclaration(PandaGen * pg,const ir::ScriptFunction * func)56 static void CompileFunctionParameterDeclaration(PandaGen *pg, const ir::ScriptFunction *func)
57 {
58 ScopeContext scopeCtx(pg, func->Scope()->ParamScope());
59
60 uint32_t index = 0;
61
62 for (const auto *param : func->Params()) {
63 LReference ref = LReference::CreateLRef(pg, param, true);
64
65 [[maybe_unused]] binder::Variable *paramVar = ref.Variable();
66
67 if (ref.Kind() == ReferenceKind::DESTRUCTURING) {
68 util::StringView name = util::Helpers::ToStringView(pg->Allocator(), index);
69 paramVar = pg->Scope()->FindLocal(name, binder::ResolveBindingOptions::BINDINGS);
70 }
71
72 ASSERT(paramVar && paramVar->IsLocalVariable());
73
74 VReg paramReg = binder::Binder::MANDATORY_PARAMS_NUMBER + index++;
75 ASSERT(paramVar->LexicalBound() || paramVar->AsLocalVariable()->Vreg() == paramReg);
76
77 // parameter has default value
78 if (param->IsAssignmentPattern()) {
79 RegScope rs(pg);
80
81 ref.Kind() == ReferenceKind::DESTRUCTURING ?
82 pg->LoadAccumulator(func, paramReg) : ref.GetValue();
83
84 auto *nonDefaultLabel = pg->AllocLabel();
85
86 if (ref.Kind() == ReferenceKind::DESTRUCTURING) {
87 auto *loadParamLabel = pg->AllocLabel();
88
89 pg->BranchIfStrictNotUndefined(func, loadParamLabel);
90 param->AsAssignmentPattern()->Right()->Compile(pg);
91 pg->Branch(func, nonDefaultLabel);
92
93 pg->SetLabel(func, loadParamLabel);
94 pg->LoadAccumulator(func, paramReg);
95
96 pg->SetLabel(func, nonDefaultLabel);
97 ref.SetValue();
98 } else {
99 pg->BranchIfStrictNotUndefined(func, nonDefaultLabel);
100
101 param->AsAssignmentPattern()->Right()->Compile(pg);
102 ref.SetValue();
103 pg->SetLabel(func, nonDefaultLabel);
104 }
105
106 continue;
107 }
108
109 if (param->IsRestElement()) {
110 pg->CopyRestArgs(param, func->Params().size() - 1);
111 } else if (ref.Kind() == ReferenceKind::DESTRUCTURING) {
112 pg->LoadAccumulator(func, paramReg);
113 } else {
114 continue;
115 }
116 ref.SetValue();
117 }
118 }
119
CompileInstanceFields(PandaGen * pg,const ir::ScriptFunction * decl)120 static void CompileInstanceFields(PandaGen *pg, const ir::ScriptFunction *decl)
121 {
122 const auto &statements = decl->Parent()->Parent()->Parent()->AsClassDefinition()->Body();
123
124 RegScope rs(pg);
125 auto thisReg = pg->AllocReg();
126 pg->GetThis(decl);
127 pg->StoreAccumulator(decl, thisReg);
128
129 for (auto const &stmt : statements) {
130 if (stmt->IsClassProperty()) {
131 const auto *prop = stmt->AsClassProperty();
132 if (!prop->Value()) {
133 pg->LoadConst(stmt, Constant::JS_UNDEFINED);
134 } else {
135 RegScope rsProp(pg);
136 prop->Value()->Compile(pg);
137 }
138
139 if (!prop->Key()->IsIdentifier()) {
140 PandaGen::Unimplemented();
141 }
142
143 pg->StoreObjByName(stmt, thisReg, prop->Key()->AsIdentifier()->Name());
144 }
145 }
146 }
147
CompileFunction(PandaGen * pg)148 static void CompileFunction(PandaGen *pg)
149 {
150 const auto *decl = pg->RootNode()->AsScriptFunction();
151
152 // TODO(szilagyia): move after super call
153 if (decl->IsConstructor()) {
154 CompileInstanceFields(pg, decl);
155 }
156
157 auto *funcParamScope = pg->TopScope()->ParamScope();
158 if (funcParamScope->NameVar()) {
159 RegScope rs(pg);
160 pg->GetFunctionObject(pg->RootNode());
161 pg->StoreAccToLexEnv(pg->RootNode(), funcParamScope->Find(funcParamScope->NameVar()->Name()), true);
162 }
163
164 pg->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION);
165 pg->FunctionEnter();
166 pg->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
167
168 if (pg->IsAsyncFunction()) {
169 CompileFunctionParameterDeclaration(pg, decl);
170 }
171
172 const ir::AstNode *body = decl->Body();
173
174 if (body->IsExpression()) {
175 body->Compile(pg);
176 pg->ExplicitReturn(decl);
177 } else {
178 CompileSourceBlock(pg, body->AsBlockStatement());
179 }
180
181 pg->FunctionExit();
182 }
183
CompileFunctionOrProgram(PandaGen * pg)184 static void CompileFunctionOrProgram(PandaGen *pg)
185 {
186 FunctionRegScope lrs(pg);
187 const auto *topScope = pg->TopScope();
188
189 if (pg->FunctionHasFinalizer()) {
190 ASSERT(topScope->IsFunctionScope());
191
192 if (!pg->IsAsyncFunction()) {
193 CompileFunctionParameterDeclaration(pg, pg->RootNode()->AsScriptFunction());
194 }
195
196 TryContext tryCtx(pg);
197 pg->FunctionInit(tryCtx.GetCatchTable());
198
199 CompileFunction(pg);
200 } else {
201 pg->FunctionInit(nullptr);
202
203 if (topScope->IsFunctionScope() || topScope->IsTSModuleScope()) {
204 CompileFunctionParameterDeclaration(pg, pg->RootNode()->AsScriptFunction());
205 CompileFunction(pg);
206 } else {
207 ASSERT(topScope->IsGlobalScope() || topScope->IsModuleScope());
208 CompileSourceBlock(pg, pg->RootNode()->AsBlockStatement());
209 }
210 }
211 }
212
Compile(PandaGen * pg)213 void Function::Compile(PandaGen *pg)
214 {
215 pg->SetFunctionKind();
216 CompileFunctionOrProgram(pg);
217 pg->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION);
218 pg->CopyFunctionArguments(pg->RootNode());
219 pg->InitializeLexEnv(pg->RootNode());
220 pg->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
221 pg->AdjustSpillInsns();
222 pg->SortCatchTables();
223 }
224
225 } // namespace panda::es2panda::compiler
226