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/assembler/assembler.h" 20 #include "ecmascript/compiler/bytecodes.h" 21 #include "ecmascript/ecma_vm.h" 22 #include "ecmascript/mem/native_area_allocator.h" 23 24 namespace panda::ecmascript::kungfu { 25 26 enum class BaselineSpecialParameter : uint8_t { 27 GLUE, 28 SP, 29 ACC, 30 PROFILE_TYPE_INFO, 31 HOTNESS_COUNTER, 32 ENV, 33 PC, 34 }; 35 36 class StackSlotOperand { 37 public: 38 enum class BaseRegister { 39 FRAME_REGISTER, 40 STACK_REGISTER, 41 }; StackSlotOperand(BaseRegister baseReg,int32_t stackOffset)42 StackSlotOperand(BaseRegister baseReg, int32_t stackOffset) 43 : baseRegister(baseReg), offset(stackOffset) {} 44 45 ~StackSlotOperand() = default; 46 GetBaseRegister()47 BaseRegister GetBaseRegister() const 48 { 49 return baseRegister; 50 } 51 GetOffset()52 int32_t GetOffset() const 53 { 54 return offset; 55 } 56 IsFrameBase()57 bool IsFrameBase() const 58 { 59 return baseRegister == BaseRegister::FRAME_REGISTER; 60 } 61 private: 62 BaseRegister baseRegister; 63 int32_t offset = 0; 64 }; 65 66 67 using MacroParameter = std::variant<int8_t, int16_t, int32_t, int64_t, BaselineSpecialParameter, StackSlotOperand>; 68 using JumpLabel = panda::ecmascript::Label; 69 70 class MacroAssembler { 71 public: MacroAssembler()72 MacroAssembler() : allocator(NativeAreaAllocator()), chunk(&allocator) {} 73 virtual ~MacroAssembler() = default; 74 virtual uint8_t *GetBegin() const = 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> ¶meters) = 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 } // namespace panda::ecmascript::kungfu 94 #endif // ECMASCRIPT_COMPILER_ASSEMBLER_MACRO_ASSEMBLER_H 95