optimizer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Copyright (c) 2021 TGEMIT Authors & Contributors
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "codeBlock.h"
  24. static bool isLiteralNumber(ExprNode* node)
  25. {
  26. ExprNodeName name = node->getExprNodeNameEnum();
  27. return name == NameFloatNode || name == NameIntNode;
  28. }
  29. static F64 getFloatValue(ExprNode* node)
  30. {
  31. if (node->getExprNodeNameEnum() == NameFloatNode)
  32. return static_cast<FloatNode*>(node)->value;
  33. return (F64)static_cast<IntNode*>(node)->value;
  34. }
  35. static S32 getIntValue(ExprNode* node)
  36. {
  37. if (node->getExprNodeNameEnum() == NameFloatNode)
  38. return (S32)static_cast<FloatNode*>(node)->value;
  39. return static_cast<IntNode*>(node)->value;
  40. }
  41. bool FloatBinaryExprNode::optimize()
  42. {
  43. // Perform constant folding
  44. if (isLiteralNumber(right) && isLiteralNumber(left))
  45. {
  46. F64 rightValue = getFloatValue(right);
  47. F64 leftValue = getFloatValue(left);
  48. F64 result = 0.0;
  49. switch (op)
  50. {
  51. case '+':
  52. result = leftValue + rightValue;
  53. break;
  54. case '-':
  55. result = leftValue - rightValue;
  56. break;
  57. case '*':
  58. result = leftValue * rightValue;
  59. break;
  60. case '/':
  61. if (rightValue != 0.0)
  62. result = leftValue / rightValue;
  63. break;
  64. }
  65. optimizedNode = FloatNode::alloc(dbgLineNumber, result);
  66. return true;
  67. }
  68. return false;
  69. }
  70. bool IntBinaryExprNode::optimize()
  71. {
  72. if (op == '%' && left->getExprNodeNameEnum() == NameVarNode && isLiteralNumber(right))
  73. {
  74. // %a % intconst
  75. S32 val = getIntValue(right);
  76. switch (val)
  77. {
  78. case 2:
  79. op = '&';
  80. optimizedNode = IntNode::alloc(dbgLineNumber, 1);
  81. return true;
  82. case 4:
  83. op = '&';
  84. optimizedNode = IntNode::alloc(dbgLineNumber, 3);
  85. return true;
  86. case 8:
  87. op = '&';
  88. optimizedNode = IntNode::alloc(dbgLineNumber, 7);
  89. return true;
  90. }
  91. }
  92. return false;
  93. }