1 // Copyright 2017 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_WASM_CODE_SPECIALIZATION_H_ 6 #define V8_WASM_CODE_SPECIALIZATION_H_ 7 8 #include "src/assembler.h" 9 #include "src/identity-map.h" 10 #include "src/wasm/wasm-objects.h" 11 12 namespace v8 { 13 namespace internal { 14 namespace wasm { 15 16 // Helper class to specialize wasm code for a specific instance, or to update 17 // code when memory / globals / tables change. 18 // This class in unhandlified, and contains a DisallowHeapAllocation field to 19 // ensure that no allocations happen while it is alive. 20 // 21 // Set up all relocations / patching that should be performed by the Relocate* / 22 // Patch* methods, then apply all changes in one step using the Apply* methods. 23 class CodeSpecialization { 24 public: 25 CodeSpecialization(Isolate*, Zone*); 26 ~CodeSpecialization(); 27 28 // Update memory references. 29 void RelocateMemoryReferences(Address old_start, uint32_t old_size, 30 Address new_start, uint32_t new_size); 31 // Update references to global variables. 32 void RelocateGlobals(Address old_start, Address new_start); 33 // Update function table size. 34 // TODO(wasm): Prepare this for more than one indirect function table. 35 void PatchTableSize(uint32_t old_size, uint32_t new_size); 36 // Update all direct call sites based on the code table in the given instance. 37 void RelocateDirectCalls(Handle<WasmInstanceObject> instance); 38 // Relocate an arbitrary object (e.g. function table). 39 void RelocateObject(Handle<Object> old_obj, Handle<Object> new_obj); 40 41 // Apply all relocations and patching to all code in the instance (wasm code 42 // and exported functions). 43 bool ApplyToWholeInstance(WasmInstanceObject*, 44 ICacheFlushMode = FLUSH_ICACHE_IF_NEEDED); 45 // Apply all relocations and patching to one wasm code object. 46 bool ApplyToWasmCode(Code*, ICacheFlushMode = FLUSH_ICACHE_IF_NEEDED); 47 48 private: 49 Address old_mem_start = 0; 50 uint32_t old_mem_size = 0; 51 Address new_mem_start = 0; 52 uint32_t new_mem_size = 0; 53 54 Address old_globals_start = 0; 55 Address new_globals_start = 0; 56 57 uint32_t old_function_table_size = 0; 58 uint32_t new_function_table_size = 0; 59 60 Handle<WasmInstanceObject> relocate_direct_calls_instance; 61 62 bool has_objects_to_relocate = false; 63 IdentityMap<Handle<Object>, ZoneAllocationPolicy> objects_to_relocate; 64 }; 65 66 } // namespace wasm 67 } // namespace internal 68 } // namespace v8 69 70 #endif // V8_WASM_CODE_SPECIALIZATION_H_ 71