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