Files
ark_js_runtime/ecmascript/stub_module.cpp
T
luochuhao c705ce054b Refactor ID definition and callSignature initialization mechanism of stubs
Refactor ID definition and callSignature initialization mechanism of runtime stub,
optimizer stub and bytecode handler stub,
reduce amount and complexity of macro used in calling those stubs.

Add stubDes struct to classify stubs both in AOT file generation phase
and AOT file loading phase.

Fix some circular dependency problems in including header files.

Fix certain inline function declaration and definition format problems, which could lead to
compiling errors.

Issue: https://gitee.com/openharmony/ark_js_runtime/issues/I4VMLD?from=project-issue
Test: stubTest(unit test), richards with asm interpreter enabled
Signed-off-by: luochuhao <luochuhao@huawei.com>
Change-Id: Ibd5fcd963347b97f8dec227f3245d2064463b0b0
2022-03-18 22:01:52 +08:00

86 lines
4.3 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "stub_module.h"
#include "ecmascript/compiler/llvm/llvm_stackmap_parser.h"
#include "ecmascript/object_factory.h"
namespace panda::ecmascript {
void StubModule::Save(const std::string &filename)
{
if (code_ != nullptr) {
std::ofstream modulefile(filename.c_str(), std::ofstream::binary);
SetStubNum(stubEntries_.size());
/* write stub entries offset */
modulefile.write(reinterpret_cast<char *>(&stubNum_), sizeof(uint64_t));
modulefile.write(reinterpret_cast<char *>(stubEntries_.data()), sizeof(StubDes) * stubNum_);
int codeSize = code_->GetInstructionSizeInBytes();
/* write host code section start addr */
modulefile.write(reinterpret_cast<char *>(&hostCodeSectionAddr_), sizeof(hostCodeSectionAddr_));
/* write stackmap offset */
int stackmapOffset = sizeof(uintptr_t) * stubNum_ + 2 * sizeof(int) + codeSize;
modulefile.write(reinterpret_cast<char *>(&stackmapOffset), sizeof(int));
/* write code length & code buff */
modulefile.write(reinterpret_cast<char *>(&codeSize), sizeof(codeSize));
modulefile.write(reinterpret_cast<char *>(code_->GetDataOffsetAddress()), codeSize);
/* write stackmap buff */
int stackmapSize = GetStackMapSize();
#ifndef NDEBUG
LOG_ECMA(INFO) << "stackmap host addr:" << stackMapAddr_ << " stackmapSize:" << stackmapSize << std::endl;
#endif
modulefile.write(reinterpret_cast<char *>(&stackmapSize), sizeof(stackmapSize));
modulefile.write(reinterpret_cast<char *>(stackMapAddr_), stackmapSize);
modulefile.close();
}
}
void StubModule::Load(JSThread *thread, const std::string &filename)
{
// now MachineCode is non movable, code and stackmap sperately is saved to MachineCode
// by calling NewMachineCodeObject.
// then MachineCode will support movable, code is saved to MachineCode and stackmap is saved
// to different heap which will be freed when stackmap is parsed by EcmaVM is started.
std::ifstream modulefile(filename.c_str(), std::ofstream::binary);
modulefile.read(reinterpret_cast<char *>(&stubNum_), sizeof(uint64_t));
stubEntries_.resize(stubNum_);
modulefile.read(reinterpret_cast<char *>(stubEntries_.data()), sizeof(StubDes) * stubNum_);
/* read host code section start addr */
modulefile.read(reinterpret_cast<char *>(&hostCodeSectionAddr_), sizeof(hostCodeSectionAddr_));
int stackmapOffset;
modulefile.read(reinterpret_cast<char *>(&stackmapOffset), sizeof(stackmapOffset));
int codeSize = 0;
modulefile.read(reinterpret_cast<char *>(&codeSize), sizeof(codeSize));
auto factory = thread->GetEcmaVM()->GetFactory();
auto codeHandle = factory->NewMachineCodeObject(codeSize, nullptr);
modulefile.read(reinterpret_cast<char *>(codeHandle->GetDataOffsetAddress()), codeSize);
SetCode(*codeHandle);
SetDeviceCodeSectionAddr(codeHandle->GetDataOffsetAddress());
/* read stackmap */
int stackmapSize;
modulefile.read(reinterpret_cast<char *>(&stackmapSize), sizeof(stackmapSize));
SetStackMapSize(stackmapSize);
std::unique_ptr<uint8_t[]> stackmapPtr(std::make_unique<uint8_t[]>(stackmapSize));
modulefile.read(reinterpret_cast<char *>(stackmapPtr.get()), stackmapSize);
if (stackmapSize != 0) {
kungfu::LLVMStackMapParser::GetInstance().CalculateStackMap(std::move(stackmapPtr),
hostCodeSectionAddr_, devicesCodeSectionAddr_);
}
for (size_t i = 0; i < stubEntries_.size(); i++) {
stubEntries_[i].codeAddr_ += code_->GetDataOffsetAddress();
}
modulefile.close();
}
} // namespace panda::ecmascript