Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT 2 : // This file is part of AsmJit project <https://asmjit.com> 3 : // 4 : // See asmjit.h or LICENSE.md for license and copyright information 5 : // SPDX-License-Identifier: Zlib 6 : 7 : #include "api-build_p.h" 8 : #ifndef ASMJIT_NO_JIT 9 : 10 : #include "cpuinfo.h" 11 : #include "jitruntime.h" 12 : 13 : ASMJIT_BEGIN_NAMESPACE 14 : 15 27422 : JitRuntime::JitRuntime(const JitAllocator::CreateParams* params) noexcept 16 27422 : : _allocator(params) { 17 27422 : _environment = Environment::host(); 18 : _environment.setObjectFormat(ObjectFormat::kJIT); 19 27422 : } 20 : 21 54844 : JitRuntime::~JitRuntime() noexcept {} 22 : 23 27492 : Error JitRuntime::_add(void** dst, CodeHolder* code) noexcept { 24 27492 : *dst = nullptr; 25 : 26 27492 : ASMJIT_PROPAGATE(code->flatten()); 27 27492 : ASMJIT_PROPAGATE(code->resolveUnresolvedLinks()); 28 : 29 27492 : size_t estimatedCodeSize = code->codeSize(); 30 27492 : if (ASMJIT_UNLIKELY(estimatedCodeSize == 0)) 31 : return DebugUtils::errored(kErrorNoCodeGenerated); 32 : 33 : uint8_t* rx; 34 : uint8_t* rw; 35 27492 : ASMJIT_PROPAGATE(_allocator.alloc((void**)&rx, (void**)&rw, estimatedCodeSize)); 36 : 37 : // Relocate the code. 38 27492 : Error err = code->relocateToBase(uintptr_t((void*)rx)); 39 27492 : if (ASMJIT_UNLIKELY(err)) { 40 0 : _allocator.release(rx); 41 0 : return err; 42 : } 43 : 44 : // Recalculate the final code size and shrink the memory we allocated for it 45 : // in case that some relocations didn't require records in an address table. 46 27492 : size_t codeSize = code->codeSize(); 47 27492 : if (codeSize < estimatedCodeSize) 48 0 : _allocator.shrink(rx, codeSize); 49 : 50 27492 : if (codeSize < estimatedCodeSize) 51 0 : _allocator.shrink(rx, codeSize); 52 : 53 : { 54 27492 : VirtMem::ProtectJitReadWriteScope rwScope(rx, codeSize); 55 : 56 54984 : for (Section* section : code->_sections) { 57 : size_t offset = size_t(section->offset()); 58 : size_t bufferSize = size_t(section->bufferSize()); 59 : size_t virtualSize = size_t(section->virtualSize()); 60 : 61 : ASMJIT_ASSERT(offset + bufferSize <= codeSize); 62 27492 : memcpy(rw + offset, section->data(), bufferSize); 63 : 64 27492 : if (virtualSize > bufferSize) { 65 : ASMJIT_ASSERT(offset + virtualSize <= codeSize); 66 0 : memset(rw + offset + bufferSize, 0, virtualSize - bufferSize); 67 : } 68 : } 69 : } 70 : 71 27492 : *dst = rx; 72 27492 : return kErrorOk; 73 : } 74 : 75 100 : Error JitRuntime::_release(void* p) noexcept { 76 100 : return _allocator.release(p); 77 : } 78 : 79 : ASMJIT_END_NAMESPACE 80 : 81 : #endif 82 : 83 : #endif // __PLUMED_HAS_ASMJIT