14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 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_BASELINE_BASELINE_COMPILER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_BASELINE_BASELINE_COMPILER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <unordered_map>
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/machine_code.h"
224514f5e3Sopenharmony_ci#include "ecmascript/compiler/baseline/baseline_assembler.h"
234514f5e3Sopenharmony_ci#include "ecmascript/compiler/ecma_opcode_des.h"
244514f5e3Sopenharmony_ci#include "ecmascript/compiler/jit_compilation_env.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ciclass BytecodeNativePcOffsetTable {
294514f5e3Sopenharmony_cipublic:
304514f5e3Sopenharmony_ci    BytecodeNativePcOffsetTable() = default;
314514f5e3Sopenharmony_ci    ~BytecodeNativePcOffsetTable() = default;
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci    void AddPosition(uint64_t nativePc)
344514f5e3Sopenharmony_ci    {
354514f5e3Sopenharmony_ci        ASSERT(nativePc - prevNativePc < 256); // 256: the max number can be presented by uint8_t
364514f5e3Sopenharmony_ci        auto nativePcDiff = static_cast<uint8_t>(nativePc - prevNativePc);
374514f5e3Sopenharmony_ci        nativePcDiffInfo.emplace_back(static_cast<uint8_t>(nativePcDiff));
384514f5e3Sopenharmony_ci        prevNativePc = nativePc;
394514f5e3Sopenharmony_ci    }
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    uint8_t *GetData()
424514f5e3Sopenharmony_ci    {
434514f5e3Sopenharmony_ci        return nativePcDiffInfo.data();
444514f5e3Sopenharmony_ci    }
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    size_t GetSize() const
474514f5e3Sopenharmony_ci    {
484514f5e3Sopenharmony_ci        return nativePcDiffInfo.size();
494514f5e3Sopenharmony_ci    }
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    uint64_t GetPrevNativePc() const
524514f5e3Sopenharmony_ci    {
534514f5e3Sopenharmony_ci        return prevNativePc;
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ciprivate:
574514f5e3Sopenharmony_ci    uint64_t prevNativePc = 0;
584514f5e3Sopenharmony_ci    std::vector<uint8_t> nativePcDiffInfo;
594514f5e3Sopenharmony_ci};
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ciclass BaselineCompiler {
624514f5e3Sopenharmony_cipublic:
634514f5e3Sopenharmony_ci    explicit BaselineCompiler(EcmaVM *inputVM, CompilationEnv *inputEnv)
644514f5e3Sopenharmony_ci        : vm(inputVM), compilationEnv(inputEnv), baselineAssembler(vm->GetJSOptions().GetTargetTriple()) {}
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    ~BaselineCompiler()
674514f5e3Sopenharmony_ci    {
684514f5e3Sopenharmony_ci        for (auto elem : jumpMap) {
694514f5e3Sopenharmony_ci            if (elem.second != nullptr) {
704514f5e3Sopenharmony_ci                delete elem.second;
714514f5e3Sopenharmony_ci                elem.second = nullptr;
724514f5e3Sopenharmony_ci            }
734514f5e3Sopenharmony_ci        }
744514f5e3Sopenharmony_ci        jumpMap.clear();
754514f5e3Sopenharmony_ci    }
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    void Compile(const JSPandaFile *jsPandaFile, const MethodLiteral *methodLiteral);
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    BaselineAssembler &GetBaselineAssembler()
804514f5e3Sopenharmony_ci    {
814514f5e3Sopenharmony_ci        return baselineAssembler;
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    bool CollectMemoryCodeInfos(MachineCodeDesc &codeDesc);
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    void SetPfHeaderAddr(const JSPandaFile *jsPandaFile);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    void GetJumpToOffsets(const uint8_t *start, const uint8_t *end, std::unordered_set<size_t> &jumpToOffsets) const;
894514f5e3Sopenharmony_ciprivate:
904514f5e3Sopenharmony_ci    EcmaVM *vm = nullptr;
914514f5e3Sopenharmony_ci    CompilationEnv *compilationEnv = nullptr;
924514f5e3Sopenharmony_ci    BaselineAssembler baselineAssembler;
934514f5e3Sopenharmony_ci    size_t bytecodeOffset = 0;
944514f5e3Sopenharmony_ci    std::unordered_map<size_t, JumpLabel*> jumpMap;
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ci#define BYTECODE_BASELINE_HANDLER(name) void Handle##name(const uint8_t *bytecodeArray);
974514f5e3Sopenharmony_ci    ECMA_OPCODE_LIST(BYTECODE_BASELINE_HANDLER)
984514f5e3Sopenharmony_ci#undef BYTECODE_BASELINE_HANDLER
994514f5e3Sopenharmony_ci    const uint8_t *pfHeaderAddr = nullptr;
1004514f5e3Sopenharmony_ci    const uint8_t *firstPC = nullptr;
1014514f5e3Sopenharmony_ci    static constexpr uint32_t ONE_BYTE_SIZE = 8;
1024514f5e3Sopenharmony_ci    static constexpr uint32_t TWO_BYTE_SIZE = 16;
1034514f5e3Sopenharmony_ci    static constexpr uint32_t THREE_BYTE_SIZE = 24;
1044514f5e3Sopenharmony_ci    BytecodeNativePcOffsetTable nativePcOffsetTable;
1054514f5e3Sopenharmony_ci};
1064514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::kungfu
1074514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_BASELINE_BASELINE_COMPILER_H
108