optimizer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "console/console.h"
  24. #include "console/codeBlock.h"
  25. static bool isLiteralNumber(ExprNode* node)
  26. {
  27. ExprNodeName name = node->getExprNodeNameEnum();
  28. return name == NameFloatNode || name == NameIntNode;
  29. }
  30. static F64 getFloatValue(ExprNode* node)
  31. {
  32. if (node->getExprNodeNameEnum() == NameFloatNode)
  33. return static_cast<FloatNode*>(node)->value;
  34. return (F64)static_cast<IntNode*>(node)->value;
  35. }
  36. static S32 getIntValue(ExprNode* node)
  37. {
  38. if (node->getExprNodeNameEnum() == NameFloatNode)
  39. return (S32)static_cast<FloatNode*>(node)->value;
  40. return static_cast<IntNode*>(node)->value;
  41. }
  42. bool FloatBinaryExprNode::optimize()
  43. {
  44. // Perform constant folding
  45. if (isLiteralNumber(right) && isLiteralNumber(left))
  46. {
  47. F64 rightValue = getFloatValue(right);
  48. F64 leftValue = getFloatValue(left);
  49. F64 result = 0.0;
  50. switch (op)
  51. {
  52. case '+':
  53. result = leftValue + rightValue;
  54. break;
  55. case '-':
  56. result = leftValue - rightValue;
  57. break;
  58. case '*':
  59. result = leftValue * rightValue;
  60. break;
  61. case '/':
  62. if (rightValue != 0.0)
  63. result = leftValue / rightValue;
  64. break;
  65. }
  66. optimizedNode = FloatNode::alloc(dbgLineNumber, result);
  67. return true;
  68. }
  69. return false;
  70. }
  71. bool IntBinaryExprNode::optimize()
  72. {
  73. if (op == '%' && left->getExprNodeNameEnum() == NameVarNode && isLiteralNumber(right))
  74. {
  75. // %a % intconst
  76. S32 val = getIntValue(right);
  77. switch (val)
  78. {
  79. case 2:
  80. op = '&';
  81. optimizedNode = IntNode::alloc(dbgLineNumber, 1);
  82. return true;
  83. case 4:
  84. op = '&';
  85. optimizedNode = IntNode::alloc(dbgLineNumber, 3);
  86. return true;
  87. case 8:
  88. op = '&';
  89. optimizedNode = IntNode::alloc(dbgLineNumber, 7);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }