| 405 | }; |
| 406 | |
| 407 | void Context::relocateCode( bool pwh ) { |
| 408 | SimNodeRelocator rel; |
| 409 | rel.context = this; |
| 410 | rel.newCode = make_shared<NodeAllocator>(); |
| 411 | rel.newCode->customGrow = [&](uint64_t ) { return uint64_t(4000); }; // because SimNode_Aot is 80 bytes |
| 412 | uint32_t codeSize = uint32_t(code->bytesAllocated()); |
| 413 | if ( code->prefixWithHeader && !pwh ) { |
| 414 | // printf("[REL] %i adjusting\n", code->totalNodesAllocated); |
| 415 | codeSize -= code->totalNodesAllocated * uint32_t(sizeof(NodePrefix)); |
| 416 | } else { |
| 417 | // printf("[REL] %i not adjusting\n", code->totalNodesAllocated); |
| 418 | } |
| 419 | rel.newCode->prefixWithHeader = pwh; |
| 420 | rel.newCode->setInitialSize(codeSize); |
| 421 | SimFunction * oldFunctions = functions; |
| 422 | if ( totalFunctions ) { |
| 423 | SimFunction * newFunctions = (SimFunction *) rel.newCode->allocate(totalFunctions*sizeof(SimFunction)); |
| 424 | memcpy ( newFunctions, functions, totalFunctions*sizeof(SimFunction)); |
| 425 | for ( int i=0, is=totalFunctions; i!=is; ++i ) { |
| 426 | newFunctions[i].name = rel.newCode->allocateName(functions[i].name); |
| 427 | newFunctions[i].mangledName = rel.newCode->allocateName(functions[i].mangledName); |
| 428 | } |
| 429 | functions = newFunctions; |
| 430 | } |
| 431 | if ( totalVariables ) { |
| 432 | GlobalVariable * newVariables = (GlobalVariable *) rel.newCode->allocate(totalVariables*sizeof(GlobalVariable)); |
| 433 | memcpy ( newVariables, globalVariables, totalVariables*sizeof(GlobalVariable)); |
| 434 | for ( int i=0, is=totalVariables; i!=is; ++i ) { |
| 435 | newVariables[i].name = rel.newCode->allocateName(globalVariables[i].name); |
| 436 | } |
| 437 | globalVariables = newVariables; |
| 438 | } |
| 439 | // relocate mangle-name lookup |
| 440 | for ( auto & kv : *tabMnLookup ) { |
| 441 | auto fn = kv.second; |
| 442 | if ( fn!=nullptr ) { |
| 443 | if ( fn>=oldFunctions && fn<(oldFunctions+totalFunctions) ) { |
| 444 | ptrdiff_t index = fn - oldFunctions; |
| 445 | kv.second = functions + index; |
| 446 | DAS_ASSERT(fn->mangledNameHash == kv.second->mangledNameHash); |
| 447 | DAS_ASSERT(kv.second>=functions && kv.second<(functions+totalFunctions)); |
| 448 | // printf("%3i - MNH 0x%8x: %s [move %p -> %p]\n", i, fn->mangledNameHash, fn->name, fn, kv.second ); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | // relocate variables |
| 453 | if ( totalVariables ) { |
| 454 | for ( int j=0, js=totalVariables; j!=js; ++j ) { |
| 455 | auto & var = globalVariables[j]; |
| 456 | if ( var.init) { |
| 457 | var.init = var.init->visit(rel); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | // relocate functions |
| 462 | for ( int i=0, is=totalFunctions; i!=is; ++i ) { |
| 463 | auto & fn = functions[i]; |
| 464 | fn.code = fn.code->visit(rel); |
no test coverage detected