StatementReader.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "RuntimeByteCodePch.h"
  6. namespace Js
  7. {
  8. void StatementReader::Create(FunctionBody * functionRead, uint startOffset /* = 0 */)
  9. {
  10. Assert(functionRead);
  11. StatementReader::Create(functionRead, startOffset, false);
  12. }
  13. void StatementReader::Create(FunctionBody* functionRead, uint startOffset, bool useOriginalByteCode)
  14. {
  15. AssertMsg(functionRead != nullptr, "Must provide valid function to execute");
  16. ByteBlock * pblkByteCode = useOriginalByteCode ?
  17. functionRead->GetOriginalByteCode() :
  18. functionRead->GetByteCode();
  19. AssertMsg(pblkByteCode != nullptr, "Must have valid byte-code to read");
  20. m_startLocation = pblkByteCode->GetBuffer();
  21. const byte * currentLocation = m_startLocation + startOffset;
  22. const bool isInDebugMode = functionRead->IsByteCodeDebugMode();
  23. m_statementMap = functionRead->GetStatementMapSpanSequence();
  24. if (m_statementMap == nullptr && isInDebugMode)
  25. {
  26. m_fullstatementMap = functionRead->GetStatementMaps();
  27. }
  28. else
  29. {
  30. m_fullstatementMap = nullptr;
  31. }
  32. if (m_statementMap && m_statementMap->Count())
  33. {
  34. m_statementMap->Reset(m_statementMapIter);
  35. m_statementIndex = 0;
  36. m_startOfStatement = true;
  37. StatementData data;
  38. if (!m_statementMap->Seek(m_statementIndex, data))
  39. {
  40. Assert(FALSE);
  41. }
  42. m_nextStatementBoundary = m_startLocation + data.bytecodeBegin;
  43. // If we starting in the middle of the function (e.g., loop body), find out where the next statement is.
  44. while (m_nextStatementBoundary < currentLocation)
  45. {
  46. this->MoveNextStatementBoundary();
  47. }
  48. }
  49. else if (m_fullstatementMap && m_fullstatementMap->Count())
  50. {
  51. m_statementIndex = 0;
  52. m_startOfStatement = true;
  53. FunctionBody::StatementMap *nextMap = Js::FunctionBody::GetNextNonSubexpressionStatementMap(m_fullstatementMap, m_statementIndex);
  54. if (!nextMap)
  55. {
  56. // set to a location that will never match
  57. m_nextStatementBoundary = currentLocation - 1;
  58. }
  59. else
  60. {
  61. m_nextStatementBoundary = m_startLocation + m_fullstatementMap->Item(m_statementIndex)->byteCodeSpan.begin;
  62. // If we starting in the middle of the function (e.g., loop body), find out where the next statement is.
  63. while (m_nextStatementBoundary < currentLocation)
  64. {
  65. this->MoveNextStatementBoundary();
  66. }
  67. }
  68. }
  69. else
  70. {
  71. // set to a location that will never match
  72. m_nextStatementBoundary = currentLocation - 1;
  73. }
  74. }
  75. inline uint32 StatementReader::MoveNextStatementBoundary()
  76. {
  77. StatementData data;
  78. uint32 retStatement = Js::Constants::NoStatementIndex;
  79. if (m_startOfStatement)
  80. {
  81. m_statementIndex++;
  82. if (m_statementMap && (uint32)m_statementIndex < m_statementMap->Count() && m_statementMap->Item(m_statementIndex, m_statementMapIter, data))
  83. {
  84. // The end boundary is the last byte of the last instruction in the previous range.
  85. // We want to track the beginning of the next instruction for AtStatementBoundary.
  86. m_nextStatementBoundary = m_startLocation + data.bytecodeBegin;
  87. // The next user statement is adjacent in the bytecode
  88. retStatement = m_statementIndex;
  89. }
  90. else if (m_fullstatementMap && m_statementIndex < m_fullstatementMap->Count())
  91. {
  92. int nextInstrStart = m_fullstatementMap->Item(m_statementIndex - 1)->byteCodeSpan.end + 1;
  93. m_nextStatementBoundary = m_startLocation + nextInstrStart;
  94. Js::FunctionBody::GetNextNonSubexpressionStatementMap(m_fullstatementMap, m_statementIndex);
  95. if (nextInstrStart == m_fullstatementMap->Item(m_statementIndex)->byteCodeSpan.begin)
  96. {
  97. retStatement = m_statementIndex;
  98. }
  99. else
  100. {
  101. m_startOfStatement = false;
  102. }
  103. }
  104. else
  105. {
  106. m_startOfStatement = false;
  107. }
  108. }
  109. else
  110. {
  111. m_startOfStatement = true;
  112. if (m_statementMap && (uint32)m_statementIndex < m_statementMap->Count() && m_statementMap->Item(m_statementIndex, m_statementMapIter, data))
  113. {
  114. // Start a range of bytecode that maps to a user statement
  115. m_nextStatementBoundary = m_startLocation + data.bytecodeBegin;
  116. retStatement = m_statementIndex;
  117. }
  118. else if (m_fullstatementMap && m_statementIndex < m_fullstatementMap->Count())
  119. {
  120. FunctionBody::StatementMap *nextMap = Js::FunctionBody::GetNextNonSubexpressionStatementMap(m_fullstatementMap, m_statementIndex);
  121. if (!nextMap)
  122. {
  123. // set to a location that will never match
  124. m_nextStatementBoundary = m_startLocation - 1;
  125. }
  126. else
  127. {
  128. // Start a range of bytecode that maps to a user statement
  129. m_nextStatementBoundary = m_startLocation + m_fullstatementMap->Item(m_statementIndex)->byteCodeSpan.begin;
  130. retStatement = m_statementIndex;
  131. }
  132. }
  133. else
  134. {
  135. // The remaining bytecode instructions do not map to a user statement, set a statementBoundary that cannot match
  136. m_nextStatementBoundary = m_startLocation - 1;
  137. }
  138. }
  139. return retStatement;
  140. }
  141. } // namespace Js