|
1 | 1 | #include "optimizerModuleConstantFold.hpp" |
2 | 2 | #include <algorithm> |
3 | 3 | #include <sstream> |
| 4 | +#include <unordered_set> |
4 | 5 |
|
5 | 6 | class OptimizerConstantFoldActionMap : public Singleton<OptimizerConstantFoldActionMap> { |
6 | 7 | public: |
@@ -46,25 +47,55 @@ class OptimizerConstantFoldActionMap : public Singleton<OptimizerConstantFoldAct |
46 | 47 | //math |
47 | 48 |
|
48 | 49 |
|
49 | | - binaryActions["+"] = [](OptimizerModuleBase::Node & node) -> void { |
50 | | - float leftArg = std::get<float>(node.children[0].value); |
51 | | - float rightArg = std::get<float>(node.children[1].value); |
52 | | - |
53 | | - node.type = InstructionType::push; |
54 | | - node.children.clear(); |
55 | | - node.constant = true; |
56 | | - node.value = leftArg + rightArg; |
57 | | - }; |
58 | | - |
59 | | - binaryActions["-"] = [](OptimizerModuleBase::Node & node) -> void { |
60 | | - float leftArg = std::get<float>(node.children[0].value); |
61 | | - float rightArg = std::get<float>(node.children[1].value); |
62 | | - |
63 | | - node.type = InstructionType::push; |
64 | | - node.children.clear(); |
65 | | - node.constant = true; |
66 | | - node.value = leftArg - rightArg; |
67 | | - }; |
| 50 | + //binaryActions["+"] = [](OptimizerModuleBase::Node & node) -> void { |
| 51 | + // if (node.children[0].value.index() == 1) { //string |
| 52 | + // auto leftArg = std::get<STRINGTYPE>(node.children[0].value); |
| 53 | + // auto rightArg = std::get<STRINGTYPE>(node.children[1].value); |
| 54 | + // node.value = leftArg + rightArg; |
| 55 | + // } else {//float |
| 56 | + // float leftArg = std::get<float>(node.children[0].value); |
| 57 | + // float rightArg = std::get<float>(node.children[1].value); |
| 58 | + // node.value = leftArg + rightArg; |
| 59 | + // } |
| 60 | + // |
| 61 | + // |
| 62 | + // |
| 63 | + // node.type = InstructionType::push; |
| 64 | + // node.children.clear(); |
| 65 | + // node.constant = true; |
| 66 | + // |
| 67 | + //}; |
| 68 | + |
| 69 | + //binaryActions["-"] = [](OptimizerModuleBase::Node & node) -> void { |
| 70 | + // if (node.children[0].value.index() == 4) { //array |
| 71 | + // //std::unordered_set<STRINGTYPE> vals; |
| 72 | + // // |
| 73 | + // //for (auto& i : node.children[1].children) { //#TODO number support |
| 74 | + // // if (i.value.index() != 1) |
| 75 | + // // return; //not string, don't optimize |
| 76 | + // // vals.emplace(std::get<STRINGTYPE>(i.value)); |
| 77 | + // //} |
| 78 | + // //std::vector<OptimizerModuleBase::Node> newNodes; |
| 79 | + // //for (auto& it : node.children[0].children) { |
| 80 | + // // if (it.value.index() != 1) |
| 81 | + // // return; //not string, don't optimize |
| 82 | + // // auto & sval = std::get<STRINGTYPE>(it.value); |
| 83 | + // // |
| 84 | + // // auto found = vals.find(sval); |
| 85 | + // // if (found == vals.end()) |
| 86 | + // // newNodes.emplace_back(std::move(it)); |
| 87 | + // //} |
| 88 | + // //node.children[0].children = std::move(newNodes); |
| 89 | + // return; |
| 90 | + // } else {//float |
| 91 | + // float leftArg = std::get<float>(node.children[0].value); |
| 92 | + // float rightArg = std::get<float>(node.children[1].value); |
| 93 | + // node.value = leftArg - rightArg; |
| 94 | + // } |
| 95 | + // node.type = InstructionType::push; |
| 96 | + // node.children.clear(); |
| 97 | + // node.constant = true; |
| 98 | + //}; |
68 | 99 |
|
69 | 100 | binaryActions["/"] = [](OptimizerModuleBase::Node & node) -> void { |
70 | 101 | float leftArg = std::get<float>(node.children[0].value); |
@@ -118,19 +149,26 @@ class OptimizerConstantFoldActionMap : public Singleton<OptimizerConstantFoldAct |
118 | 149 | } |
119 | 150 |
|
120 | 151 | void setupNulary() { |
121 | | - nularyActions["true"] = [](OptimizerModuleBase::Node & node) -> void { |
122 | | - node.type = InstructionType::push; |
123 | | - node.children.clear(); |
124 | | - node.constant = true; |
125 | | - node.value = true; |
126 | | - }; |
127 | | - |
128 | | - nularyActions["false"] = [](OptimizerModuleBase::Node & node) -> void { |
129 | | - node.type = InstructionType::push; |
130 | | - node.children.clear(); |
131 | | - node.constant = true; |
132 | | - node.value = false; |
133 | | - }; |
| 152 | + //nularyActions["true"] = [](OptimizerModuleBase::Node & node) -> void { |
| 153 | + // node.type = InstructionType::push; |
| 154 | + // node.children.clear(); |
| 155 | + // node.constant = true; |
| 156 | + // node.value = true; |
| 157 | + //}; |
| 158 | + // |
| 159 | + //nularyActions["false"] = [](OptimizerModuleBase::Node & node) -> void { |
| 160 | + // node.type = InstructionType::push; |
| 161 | + // node.children.clear(); |
| 162 | + // node.constant = true; |
| 163 | + // node.value = false; |
| 164 | + //}; |
| 165 | + |
| 166 | + //nularyActions["nil"] = [](OptimizerModuleBase::Node & node) -> void { |
| 167 | + // node.type = InstructionType::push; |
| 168 | + // node.children.clear(); |
| 169 | + // node.constant = true; |
| 170 | + // node.value = false;//#TODO |
| 171 | + //}; |
134 | 172 | } |
135 | 173 |
|
136 | 174 | std::unordered_map<std::string, std::function<void(OptimizerModuleBase::Node&)>> binaryActions; |
@@ -181,6 +219,7 @@ void OptimizerModuleConstantFold::processNode(Node& node) { |
181 | 219 | case InstructionType::assignToLocal: break; |
182 | 220 | case InstructionType::getVariable: break; |
183 | 221 | case InstructionType::makeArray: { |
| 222 | + return; |
184 | 223 | if (node.areChildrenConstant()) {//#TODO when converting to ASM check again if all elements are push |
185 | 224 | bool allPush = std::all_of(node.children.begin(), node.children.end(), [](const Node & it) |
186 | 225 | { |
|
0 commit comments