Files
kobalicek b56f4176cb Codebase update and improvements, instruction DB update
* Denested src folder to root, renamed testing to asmjit-testing

  * Refactored how headers are included into <asmjit/...> form. This
    is necessary as compilers would never simplify a path once a ..
    appears in include directory - then paths such as ../core/../core
    appeared in asserts, which was ugly

  * Moved support utilities into asmjit/support/... (still included
    by asmjit/core.h for convenience and compatibility)

  * Added CMakePresets.json for making it easy to develop AsmJit

  * Reworked CMakeLists to be shorter and use CMake option(),
    etc... This simplifies it and makes it using more standard
    features

  * ASMJIT_EMBED now creates asmjit_embed INTERFACE library,
    which is accessible via asmjit::asmjit target - this simplifies
    embedding and makes it the same as library targets from a CMake
    perspective

  * Removed ASMJIT_DEPS - this is now provided by cmake target
    aliases - 'asmjit::asmjit' so users should not need this variable

  * Changed meaning of ASMJIT_LIBS - this now contains only AsmJit
    dependencies without asmjit::asmjit target alias. Don't rely on
    ASMJIT_LIBS anymore as it's only used internally

  * Removed ASMJIT_NO_DEPRECATED option - AsmJit is not going
    to provide controllable deprecations in the future

  * Removed ASMJIT_NO_VALIDATION in favor of ASMJIT_NO_INTROSPECTION,
    which now controls query, features, and validation API presence

  * Removed ASMJIT_DIR option - it was never really needed

  * Removed AMX_TRANSPOSE feature from instruction database (X86).
    Intel has removed it as well, so it's a feature that won't
    be siliconized
2025-11-02 22:31:46 +01:00

87 lines
2.6 KiB
C++

// This file is part of AsmJit project <https://asmjit.com>
//
// See <asmjit/core.h> or LICENSE.md for license and copyright information
// SPDX-License-Identifier: Zlib
#include <asmjit/core/api-build_p.h>
#ifndef ASMJIT_NO_JIT
#include <asmjit/core/cpuinfo.h>
#include <asmjit/core/jitruntime.h>
ASMJIT_BEGIN_NAMESPACE
JitRuntime::JitRuntime(const JitAllocator::CreateParams* params) noexcept
: _allocator(params) {
_environment = Environment::host();
_environment.set_object_format(ObjectFormat::kJIT);
const CpuInfo& host_cpu = CpuInfo::host();
_cpu_features = host_cpu.features();
_cpu_hints = host_cpu.hints();
}
JitRuntime::~JitRuntime() noexcept {}
Error JitRuntime::_add(void** dst, CodeHolder* code) noexcept {
*dst = nullptr;
ASMJIT_PROPAGATE(code->flatten());
ASMJIT_PROPAGATE(code->resolve_cross_section_fixups());
size_t estimated_code_size = code->code_size();
if (ASMJIT_UNLIKELY(estimated_code_size == 0)) {
return make_error(Error::kNoCodeGenerated);
}
JitAllocator::Span span;
ASMJIT_PROPAGATE(_allocator.alloc(Out(span), estimated_code_size));
// Relocate the code.
CodeHolder::RelocationSummary relocation_summary;
Error err = code->relocate_to_base(uintptr_t(span.rx()), &relocation_summary);
if (ASMJIT_UNLIKELY(err != Error::kOk)) {
_allocator.release(span.rx());
return err;
}
// Recalculate the final code size and shrink the memory we allocated for it
// in case that some relocations didn't require records in an address table.
size_t code_size = estimated_code_size - relocation_summary.code_size_reduction;
// If not true it means that `relocate_to_base()` filled wrong information in `relocation_summary`.
ASMJIT_ASSERT(code_size == code->code_size());
_allocator.write(span, [&](JitAllocator::Span& span) noexcept -> Error {
uint8_t* rw = static_cast<uint8_t*>(span.rw());
for (Section* section : code->_sections) {
size_t offset = size_t(section->offset());
size_t buffer_size = size_t(section->buffer_size());
size_t virtual_size = size_t(section->virtual_size());
ASMJIT_ASSERT(offset + buffer_size <= span.size());
memcpy(rw + offset, section->data(), buffer_size);
if (virtual_size > buffer_size) {
ASMJIT_ASSERT(offset + virtual_size <= span.size());
memset(rw + offset + buffer_size, 0, virtual_size - buffer_size);
}
}
span.shrink(code_size);
return Error::kOk;
});
*dst = span.rx();
return Error::kOk;
}
Error JitRuntime::_release(void* p) noexcept {
return _allocator.release(p);
}
ASMJIT_END_NAMESPACE
#endif