JavascriptNativeOperators.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. namespace Js
  6. {
  7. #if ENABLE_NATIVE_CODEGEN
  8. template <typename T>
  9. class BranchDictionaryWrapper
  10. {
  11. public:
  12. class DictAllocator :public NativeCodeData::Allocator
  13. {
  14. public:
  15. char * Alloc(size_t requestedBytes)
  16. {
  17. char* dataBlock = __super::Alloc(requestedBytes);
  18. #if DBG
  19. if (JITManager::GetJITManager()->IsJITServer())
  20. {
  21. NativeCodeData::DataChunk* chunk = NativeCodeData::GetDataChunk(dataBlock);
  22. chunk->dataType = "BranchDictionary::Bucket";
  23. if (PHASE_TRACE1(Js::NativeCodeDataPhase))
  24. {
  25. Output::Print(_u("NativeCodeData BranchDictionary::Bucket: chunk: %p, data: %p, index: %d, len: %x, totalOffset: %x, type: %S\n"),
  26. chunk, (void*)dataBlock, chunk->allocIndex, chunk->len, chunk->offset, chunk->dataType);
  27. }
  28. }
  29. #endif
  30. return dataBlock;
  31. }
  32. char * AllocZero(size_t requestedBytes)
  33. {
  34. char* dataBlock = __super::AllocZero(requestedBytes);
  35. #if DBG
  36. if (JITManager::GetJITManager()->IsJITServer())
  37. {
  38. NativeCodeData::DataChunk* chunk = NativeCodeData::GetDataChunk(dataBlock);
  39. chunk->dataType = "BranchDictionary::Entries";
  40. if (PHASE_TRACE1(Js::NativeCodeDataPhase))
  41. {
  42. Output::Print(_u("NativeCodeData BranchDictionary::Entries: chunk: %p, data: %p, index: %d, len: %x, totalOffset: %x, type: %S\n"),
  43. chunk, (void*)dataBlock, chunk->allocIndex, chunk->len, chunk->offset, chunk->dataType);
  44. }
  45. }
  46. #endif
  47. return dataBlock;
  48. }
  49. };
  50. template <class TKey, class TValue>
  51. class SimpleDictionaryEntryWithFixUp : public JsUtil::SimpleDictionaryEntry<TKey, TValue>
  52. {
  53. public:
  54. void FixupWithRemoteKey(void* remoteKey)
  55. {
  56. this->key = (TKey)remoteKey;
  57. }
  58. };
  59. typedef JsUtil::BaseDictionary<T, void*, DictAllocator, PowerOf2SizePolicy, DefaultComparer, SimpleDictionaryEntryWithFixUp> BranchBaseDictionary;
  60. class BranchDictionary :public BranchBaseDictionary
  61. {
  62. public:
  63. BranchDictionary(DictAllocator* allocator, uint dictionarySize)
  64. : BranchBaseDictionary(allocator, dictionarySize)
  65. {
  66. }
  67. void Fixup(NativeCodeData::DataChunk* chunkList, void** remoteKeys)
  68. {
  69. for (int i = 0; i < this->Count(); i++)
  70. {
  71. this->entries[i].FixupWithRemoteKey(remoteKeys[i]);
  72. }
  73. FixupNativeDataPointer(buckets, chunkList);
  74. FixupNativeDataPointer(entries, chunkList);
  75. }
  76. };
  77. BranchDictionaryWrapper(NativeCodeData::Allocator * allocator, uint dictionarySize, ArenaAllocator* remoteKeyAlloc) :
  78. defaultTarget(nullptr), dictionary((DictAllocator*)allocator, dictionarySize)
  79. {
  80. if (remoteKeyAlloc)
  81. {
  82. remoteKeys = AnewArrayZ(remoteKeyAlloc, void*, dictionarySize);
  83. }
  84. else
  85. {
  86. Assert(!JITManager::GetJITManager()->IsJITServer());
  87. remoteKeys = nullptr;
  88. }
  89. }
  90. BranchDictionary dictionary;
  91. void* defaultTarget;
  92. void** remoteKeys;
  93. static BranchDictionaryWrapper* New(NativeCodeData::Allocator * allocator, uint dictionarySize, ArenaAllocator* remoteKeyAlloc)
  94. {
  95. return NativeCodeDataNew(allocator, BranchDictionaryWrapper, allocator, dictionarySize, remoteKeyAlloc);
  96. }
  97. void AddEntry(uint32 offset, T key, void* remoteVar)
  98. {
  99. int index = dictionary.AddNew(key, (void**)offset);
  100. if (JITManager::GetJITManager()->IsJITServer())
  101. {
  102. Assert(remoteKeys);
  103. remoteKeys[index] = remoteVar;
  104. }
  105. }
  106. void Fixup(NativeCodeData::DataChunk* chunkList)
  107. {
  108. if (JITManager::GetJITManager()->IsJITServer())
  109. {
  110. dictionary.Fixup(chunkList, remoteKeys);
  111. }
  112. }
  113. };
  114. class JavascriptNativeOperators
  115. {
  116. public:
  117. static void * Op_SwitchStringLookUp(JavascriptString* str, Js::BranchDictionaryWrapper<Js::JavascriptString*>* stringDictionary, uintptr_t funcStart, uintptr_t funcEnd);
  118. };
  119. #endif
  120. };