14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <string>
204514f5e3Sopenharmony_ci#include <vector>
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ci#include "ecmascript/compiler/assembler/assembler.h"
234514f5e3Sopenharmony_ci#include "ecmascript/compiler/call_signature.h"
244514f5e3Sopenharmony_ci#include "ecmascript/compiler/rt_call_signature.h"
254514f5e3Sopenharmony_ci#include "ecmascript/frames.h"
264514f5e3Sopenharmony_ci#include "ecmascript/stubs/runtime_stub_list.h"
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
294514f5e3Sopenharmony_ciclass CompilationConfig;
304514f5e3Sopenharmony_ciclass AssemblerModule {
314514f5e3Sopenharmony_cipublic:
324514f5e3Sopenharmony_ci    AssemblerModule() = default;
334514f5e3Sopenharmony_ci    ~AssemblerModule()
344514f5e3Sopenharmony_ci    {
354514f5e3Sopenharmony_ci        for (auto it : symbolTable_) {
364514f5e3Sopenharmony_ci            delete it.second;
374514f5e3Sopenharmony_ci        }
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ci    void Run(const CompilationConfig *cfg, Chunk* chunk);
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    size_t GetFunctionCount() const
434514f5e3Sopenharmony_ci    {
444514f5e3Sopenharmony_ci        return symbolTable_.size();
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    size_t GetFunction(int id) const
484514f5e3Sopenharmony_ci    {
494514f5e3Sopenharmony_ci        panda::ecmascript::Label *label = GetFunctionLabel(id);
504514f5e3Sopenharmony_ci        if (label->IsBound()) {
514514f5e3Sopenharmony_ci            return label->GetPos();
524514f5e3Sopenharmony_ci        } else {
534514f5e3Sopenharmony_ci            LOG_ECMA(FATAL) << "this branch is unreachable";
544514f5e3Sopenharmony_ci            UNREACHABLE();
554514f5e3Sopenharmony_ci        }
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    panda::ecmascript::Label* GetFunctionLabel(int id) const
594514f5e3Sopenharmony_ci    {
604514f5e3Sopenharmony_ci        return symbolTable_.at(id);
614514f5e3Sopenharmony_ci    }
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    uint8_t* GetBuffer() const
644514f5e3Sopenharmony_ci    {
654514f5e3Sopenharmony_ci        return buffer_;
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    size_t GetBufferSize() const
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        return bufferSize_;
714514f5e3Sopenharmony_ci    }
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci    void SetUpForAsmStubs();
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    const std::vector<const CallSignature*> &GetCSigns() const
764514f5e3Sopenharmony_ci    {
774514f5e3Sopenharmony_ci        return asmCallSigns_;
784514f5e3Sopenharmony_ci    }
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    const CallSignature *GetCSign(size_t i) const
814514f5e3Sopenharmony_ci    {
824514f5e3Sopenharmony_ci        return asmCallSigns_[i];
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    const std::vector<size_t> &GetStubsOffset() const
864514f5e3Sopenharmony_ci    {
874514f5e3Sopenharmony_ci        return stubsOffset_;
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    void GenerateStubsX64(Chunk* chunk);
914514f5e3Sopenharmony_ci    void GenerateStubsAarch64(Chunk* chunk);
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    static bool IsCallNew(JSCallMode mode);
944514f5e3Sopenharmony_ci    static int GetArgcFromJSCallMode(JSCallMode mode);
954514f5e3Sopenharmony_ci    static bool JSModeHaveThisArg(JSCallMode mode);
964514f5e3Sopenharmony_ci    static bool JSModeHaveNewTargetArg(JSCallMode mode);
974514f5e3Sopenharmony_ci    static bool IsJumpToCallCommonEntry(JSCallMode mode);
984514f5e3Sopenharmony_ciprivate:
994514f5e3Sopenharmony_ci    std::vector<const CallSignature *> asmCallSigns_;
1004514f5e3Sopenharmony_ci    std::map<int, panda::ecmascript::Label *> symbolTable_;
1014514f5e3Sopenharmony_ci    std::vector<size_t> stubsOffset_;
1024514f5e3Sopenharmony_ci    uint8_t* buffer_ {nullptr};
1034514f5e3Sopenharmony_ci    size_t bufferSize_ {0};
1044514f5e3Sopenharmony_ci};
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ciclass AssemblerStub {
1074514f5e3Sopenharmony_cipublic:
1084514f5e3Sopenharmony_ci    virtual void GenerateX64(Assembler* assembler) = 0;
1094514f5e3Sopenharmony_ci    virtual void GenerateAarch64(Assembler* assembler) = 0;
1104514f5e3Sopenharmony_ci    virtual ~AssemblerStub() = default;
1114514f5e3Sopenharmony_ci};
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci#define DECLARE_ASM_STUB_CLASS(name)                        \
1144514f5e3Sopenharmony_ciclass name##Stub : public AssemblerStub {                   \
1154514f5e3Sopenharmony_cipublic:                                                     \
1164514f5e3Sopenharmony_ci    ~name##Stub() = default;                                \
1174514f5e3Sopenharmony_ci    void GenerateX64(Assembler* assembler) override;        \
1184514f5e3Sopenharmony_ci    void GenerateAarch64(Assembler* assembler) override;    \
1194514f5e3Sopenharmony_ci};
1204514f5e3Sopenharmony_ciRUNTIME_ASM_STUB_LIST(DECLARE_ASM_STUB_CLASS)
1214514f5e3Sopenharmony_ci#undef DECLARE_ASM_STUB_CLASS
1224514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::kunfu
1234514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_ASSEMBLER_MODULE_H
124