1 /*
2 * Copyright (c) 2021 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 #include "ecmascript/compiler/stub.h"
16 #include "ecmascript/compiler/stub_builder.h"
17
18 namespace panda::ecmascript::kungfu {
Stub(const CallSignature * callSignature,Circuit * circuit)19 Stub::Stub(const CallSignature *callSignature, Circuit *circuit)
20 : callSignature_(callSignature),
21 builder_(circuit),
22 acc_(circuit),
23 env_(callSignature->GetParametersCount(), &builder_)
24 {
25 ASSERT(callSignature_->HasConstructor());
26 stubBuilder_ = static_cast<StubBuilder*>(callSignature_->GetConstructor()(&env_));
27 }
28
InitializeArguments()29 void Stub::InitializeArguments()
30 {
31 auto argLength = callSignature_->GetParametersCount();
32 auto paramsType = callSignature_->GetParametersType();
33 for (size_t i = 0; i < argLength; i++) {
34 GateRef argument = env_.GetArgument(i);
35 if (paramsType[i] == VariableType::NATIVE_POINTER()) {
36 auto type = env_.IsArch64Bit() ? MachineType::I64 : MachineType::I32;
37 acc_.SetMachineType(argument, type);
38 } else {
39 acc_.SetMachineType(argument, paramsType[i].GetMachineType());
40 }
41 acc_.SetGateType(argument, paramsType[i].GetGateType());
42 }
43 }
44
GenerateCircuit(const CompilationConfig * cfg)45 void Stub::GenerateCircuit(const CompilationConfig *cfg)
46 {
47 env_.SetCompilationConfig(cfg);
48 InitializeArguments();
49 stubBuilder_->GenerateCircuit();
50 }
51
~Stub()52 Stub::~Stub()
53 {
54 if (stubBuilder_ != nullptr) {
55 delete stubBuilder_;
56 stubBuilder_ = nullptr;
57 }
58 }
59 } // namespace panda::ecmascript::kungfu