1 /* 2 * Copyright (c) 2021 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 PANDA_RUNTIME_ECMASCRIPT_MEM_MACHINE_CODE_H 17 #define PANDA_RUNTIME_ECMASCRIPT_MEM_MACHINE_CODE_H 18 19 #include "ecmascript/ecma_macros.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/mem/tagged_object.h" 22 23 namespace panda { 24 namespace ecmascript { 25 class MachineCode : public TaggedObject { 26 public: 27 NO_COPY_SEMANTIC(MachineCode); 28 NO_MOVE_SEMANTIC(MachineCode); Cast(ObjectHeader * object)29 static MachineCode *Cast(ObjectHeader *object) 30 { 31 ASSERT(JSTaggedValue(object).IsMachineCodeObject()); 32 return static_cast<MachineCode *>(object); 33 } 34 35 static constexpr size_t INS_SIZE_OFFSET = TaggedObjectSize(); 36 ACCESSORS_PRIMITIVE_FIELD(InstructionSizeInBytes, uint32_t, INS_SIZE_OFFSET, LAST_OFFSET); 37 DEFINE_ALIGN_SIZE(LAST_OFFSET); 38 static constexpr size_t DATA_OFFSET = SIZE; 39 DECL_DUMP()40 DECL_DUMP() 41 42 uintptr_t GetDataOffsetAddress(void) 43 { 44 return reinterpret_cast<uintptr_t>(this) + DATA_OFFSET; 45 } 46 SetData(const uint8_t * stackMapData,size_t codeLength)47 void SetData(const uint8_t *stackMapData, size_t codeLength) 48 { 49 if (stackMapData == nullptr) { 50 LOG_ECMA_MEM(ERROR) << "data is null in creating new code object"; 51 return; 52 } 53 if (memcpy_s(reinterpret_cast<void *>(this->GetDataOffsetAddress()), 54 this->GetInstructionSizeInBytes(), stackMapData, codeLength) != EOK) { 55 LOG_ECMA_MEM(ERROR) << "memcpy fail in creating new code object "; 56 return; 57 } 58 } 59 VisitRangeSlot(const EcmaObjectRangeVisitor & v)60 void VisitRangeSlot(const EcmaObjectRangeVisitor &v) 61 { 62 // left blank deliberately,only need to visit TaggedObject type object. 63 } 64 VisitObjects(const EcmaObjectRangeVisitor & visitor)65 void VisitObjects([[maybe_unused]] const EcmaObjectRangeVisitor &visitor) const 66 { 67 // left blank deliberately,only need to visit TaggedObject type object. 68 } 69 GetMachineCodeObjectSize(void)70 size_t GetMachineCodeObjectSize(void) 71 { 72 return SIZE + this->GetInstructionSizeInBytes(); 73 } 74 }; 75 } // namespace ecmascript 76 } // namespace panda 77 78 #endif // PANDA_RUNTIME_ECMASCRIPT_MEM_MACHINE_CODE_H 79