CaseNode.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #pragma once
  6. using namespace JsUtil;
  7. /*
  8. * CaseNode - represents the case statements (not the case block) in the switch statement
  9. */
  10. class CaseNode
  11. {
  12. private:
  13. uint32 offset; // offset - indicates the bytecode offset of the case instruction
  14. uint32 targetOffset; // targetOffset - indicates the bytecode offset of the target instruction (case block)
  15. IR::BranchInstr* caseInstr; // caseInstr - stores the case instruction
  16. IR::Opnd* lowerBound; // lowerBound - used for integer cases
  17. public:
  18. CaseNode(IR::BranchInstr* caseInstr, uint32 offset, uint32 targetOffset, IR::Opnd* lowerBound = nullptr)
  19. : caseInstr(caseInstr),
  20. offset(offset),
  21. targetOffset(targetOffset),
  22. lowerBound(lowerBound)
  23. {
  24. }
  25. int32 GetSrc2IntConst()
  26. {
  27. AssertMsg(caseInstr->GetSrc2()->GetStackSym()->IsIntConst(),"Source2 operand is not an integer constant");
  28. return caseInstr->GetSrc2()->GetStackSym()->GetIntConstValue();
  29. }
  30. Js::JavascriptString* GetSrc2StringConst()
  31. {
  32. AssertMsg(caseInstr->GetSrc2()->GetStackSym()->m_isStrConst,"Source2 operand is not an integer constant");
  33. return Js::JavascriptString::FromVar(caseInstr->GetSrc2()->GetStackSym()->GetConstAddress());
  34. }
  35. bool IsSrc2IntConst()
  36. {
  37. return caseInstr->GetSrc2()->GetStackSym()->IsIntConst();
  38. }
  39. bool IsSrc2StrConst()
  40. {
  41. return caseInstr->GetSrc2()->GetStackSym()->m_isStrConst;
  42. }
  43. uint32 GetOffset()
  44. {
  45. return offset;
  46. }
  47. uint32 GetTargetOffset()
  48. {
  49. return targetOffset;
  50. }
  51. IR::Opnd* GetUpperBound()
  52. {
  53. return caseInstr->GetSrc2();
  54. }
  55. IR::Opnd* GetLowerBound()
  56. {
  57. return lowerBound;
  58. }
  59. void SetLowerBound(IR::Opnd* lowerBound)
  60. {
  61. this->lowerBound = lowerBound;
  62. }
  63. IR::BranchInstr* GetCaseInstr()
  64. {
  65. return caseInstr;
  66. }
  67. };
  68. template <>
  69. struct DefaultComparer<CaseNode *>
  70. {
  71. public:
  72. static int Compare(CaseNode* caseNode1, CaseNode* caseNode2);
  73. static bool Equals(CaseNode* x, CaseNode* y);
  74. static uint GetHashCode(CaseNode * caseNode);
  75. };