AsmJsCodeGenerator.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include "RuntimeLanguagePch.h"
  6. #ifdef ASMJS_PLAT
  7. #include "CodeGenAllocators.h"
  8. namespace Js
  9. {
  10. AsmJsCodeGenerator::AsmJsCodeGenerator( ScriptContext* scriptContext ) :
  11. mScriptContext( scriptContext )
  12. ,mPageAllocator(scriptContext->GetThreadContext()->GetPageAllocator())
  13. {
  14. //use the same foreground allocator as NativeCodeGen
  15. mForegroundAllocators = GetForegroundAllocator(scriptContext->GetNativeCodeGenerator(),mPageAllocator);
  16. mEncoder.SetPageAllocator( mPageAllocator );
  17. mEncoder.SetCodeGenAllocator( mForegroundAllocators );
  18. }
  19. void AsmJsCodeGenerator::CodeGen( FunctionBody* functionBody )
  20. {
  21. AsmJsFunctionInfo* asmInfo = functionBody->GetAsmJsFunctionInfo();
  22. Assert( asmInfo );
  23. void* address = mEncoder.Encode( functionBody );
  24. if( address )
  25. {
  26. FunctionEntryPointInfo* funcEntrypointInfo = (FunctionEntryPointInfo*)functionBody->GetDefaultEntryPointInfo();
  27. EntryPointInfo* entrypointInfo = (EntryPointInfo*)funcEntrypointInfo;
  28. Assert(entrypointInfo->GetIsAsmJSFunction());
  29. //set entrypointinfo address and nativeAddress with TJ address
  30. Js::JavascriptMethod method = reinterpret_cast<Js::JavascriptMethod>(address);
  31. entrypointInfo->jsMethod = method;
  32. entrypointInfo->SetTJNativeAddress(method, mScriptContext->GetNativeCodeGenerator());
  33. #if ENABLE_DEBUG_CONFIG_OPTIONS
  34. funcEntrypointInfo->SetIsTJMode(true);
  35. #endif
  36. if (!PreReservedVirtualAllocWrapper::IsInRange((void*)mScriptContext->GetThreadContext()->GetPreReservedRegionAddr(), (void*)address))
  37. {
  38. Assert(entrypointInfo->GetCodeSize() < (uint64)((uint64)1 << 32));
  39. mScriptContext->GetJitFuncRangeCache()->AddFuncRange((void*)address, (uint)entrypointInfo->GetCodeSize());
  40. }
  41. }
  42. }
  43. }
  44. #endif