1/*
2 * Copyright (c) 2024 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#ifndef ECMASCRIPT_COMPILER_ASSEMBLER_MACRO_ASSEMBLER_H
17#define ECMASCRIPT_COMPILER_ASSEMBLER_MACRO_ASSEMBLER_H
18
19#include "ecmascript/compiler/bytecodes.h"
20#include "ecmascript/ecma_vm.h"
21#include "ecmascript/mem/native_area_allocator.h"
22
23namespace panda::ecmascript::kungfu {
24
25enum class BaselineSpecialParameter : uint8_t {
26    GLUE,
27    SP,
28    ACC,
29    PROFILE_TYPE_INFO,
30    HOTNESS_COUNTER,
31    ENV,
32    PC,
33};
34
35class StackSlotOperand {
36public:
37    enum class BaseRegister {
38        FRAME_REGISTER,
39        STACK_REGISTER,
40    };
41    StackSlotOperand(BaseRegister baseReg, int32_t stackOffset)
42        : baseRegister(baseReg), offset(stackOffset) {}
43
44    ~StackSlotOperand() = default;
45
46    BaseRegister GetBaseRegister() const
47    {
48        return baseRegister;
49    }
50
51    int32_t GetOffset() const
52    {
53        return offset;
54    }
55
56    bool IsFrameBase() const
57    {
58        return baseRegister == BaseRegister::FRAME_REGISTER;
59    }
60private:
61    BaseRegister baseRegister;
62    int32_t offset = 0;
63};
64
65
66using MacroParameter = std::variant<int8_t, int16_t, int32_t, int64_t, BaselineSpecialParameter, StackSlotOperand>;
67using JumpLabel = panda::ecmascript::Label;
68
69class MacroAssembler {
70public:
71    MacroAssembler() : allocator(NativeAreaAllocator()), chunk(&allocator) {}
72    virtual ~MacroAssembler() = default;
73    virtual uint8_t *GetBegin() const = 0;
74    virtual size_t GetBufferCurrentSize() const = 0;
75    virtual void Move(const StackSlotOperand &dstStackSlot, Immediate value) = 0;
76    virtual void Move(const StackSlotOperand &dstStackSlot,
77                      const StackSlotOperand &srcStackSlot) = 0;
78    virtual void Cmp(const StackSlotOperand &stackSlot, Immediate value) = 0;
79    virtual void Bind(JumpLabel &label) = 0;
80    virtual void Jz(JumpLabel &label) = 0;
81    virtual void Jnz(JumpLabel &label) = 0;
82    virtual void Jump(JumpLabel &label) = 0;
83    virtual void SaveReturnRegister(const StackSlotOperand &dstStackSlot) = 0;
84    virtual void CallBuiltin(Address funcAddress,
85                             const std::vector<MacroParameter> &parameters) = 0;
86
87protected:
88    NativeAreaAllocator allocator;
89    Chunk chunk;
90    static constexpr int32_t FUNCTION_OFFSET_FROM_SP = -72; // 72: includes 9 slots
91};
92}  // namespace panda::ecmascript::kungfu
93#endif  // ECMASCRIPT_COMPILER_ASSEMBLER_MACRO_ASSEMBLER_H
94