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