-
Notifications
You must be signed in to change notification settings - Fork 0
/
Middleend.cpp
361 lines (296 loc) · 11.8 KB
/
Middleend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "Middleend.hpp"
#include "File.hpp"
Middleend::Middleend()
: analyser()
{
}
void Middleend::MiddleendDtor()
{
analyser.AbstractSyntaxTreeDumpAnalyserDtor();
}
void Middleend::optimize(const char *abstractSyntaxTreeFileName)
{
char *buffer = readFileToBuffer(abstractSyntaxTreeFileName, false);
analyser.analyseText(buffer);
std::free(buffer);
buffer = nullptr;
lookForDifferentiationOperatorRecursively(analyser.tree.root);
analyser.tree.dumpToText(abstractSyntaxTreeFileName);
}
void Middleend::lookForDifferentiationOperatorRecursively(AbstractSyntaxTree::Node *node)
{
if (node == nullptr) return;
if ((node->type == AbstractSyntaxTree::Node::Type::FunctionCallOperator)
&& (std::strcmp(node->data.identifier, "differentiate") == 0)) {
differentiateNode(node);
}
}
#if 0
AbstractSyntaxTree::Node *Middleend::differentiateNodeRecursively(const AbstractSyntaxTree::Node *node)
{
#define L node->left
#define R node->right
#define TYPE node->type
#define lTYPE node->left->type
#define rTYPE node->right->type
#define dL differentiateNodeRecursively(L)
#define dR differentiateNodeRecursively(R)
#define cL AbstractSyntaxTree::copySubtreeRecursively(L)
#define cR AbstractSyntaxTree::copySubtreeRecursively(R)
#define CREATE_MATHEMATICAL_OPERATION_NODE(operation, leftNode, rightNode) \
AbstractSyntaxTree::createMathematicalOperationNode((operation), (leftNode), (rightNode))
#define CONSTANT(constant) AbstractSyntaxTree::createNumberNode(constant)
#define ADDITION(leftNode, rightNode) CREATE_MATHEMATICAL_OPERATION_NODE('+', (leftNode), (rightNode))
#define SUBTRACTION(leftNode, rightNode) CREATE_MATHEMATICAL_OPERATION_NODE('-', (leftNode), (rightNode))
#define MULTIPLICATION(leftNode, rightNode) CREATE_MATHEMATICAL_OPERATION_NODE('*', (leftNode), (rightNode))
#define DIVISION(leftNode, rightNode) CREATE_MATHEMATICAL_OPERATION_NODE('/', (leftNode), (rightNode))
#define POWER(leftNode, rightNode) CREATE_MATHEMATICAL_OPERATION_NODE('^', (leftNode), (rightNode))
switch (TYPE) {
case AbstractSyntaxTree::Node::: {
auto resultNode = CONSTANT(0);
END_DIFFERENTIATION("Zero the derivative of a constant everyday");
return
resultNode;
}
case Variable: {
auto resultNode = CONSTANT(1);
END_DIFFERENTIATION("Formula from \"An Introduction to Statistical Learning: With Applications in R\"");
return
resultNode;
}
case Operation: {
switch (node->data) {
case Addition: {
auto resultNode = ADDITION(dL, dR);
END_DIFFERENTIATION("No matter addition or substraction - the differentiation operator is linear");
return
resultNode;
}
case Subtraction: {
auto resultNode = SUBTRACTION(dL, dR);
END_DIFFERENTIATION("No matter addition or substraction - the differentiation operator is linear");
return
resultNode;
}
case Multiplication: {
auto resultNode = ADDITION(MULTIPLICATION(dL, cR), MULTIPLICATION(cL, dR));
END_DIFFERENTIATION("At least we dont need the general Leibniz rule here");
return
resultNode;
}
case Division: {
auto resultNode = DIVISION(SUBTRACTION(MULTIPLICATION(dL, cR), MULTIPLICATION(cL, dR)), POWER(cR, CONSTANT(2)));
END_DIFFERENTIATION("Divide and conquer");
return
resultNode;
}
case Power: {
BinaryTree::Node *resultNode = nullptr;
if (((lTYPE == Variable) || lTYPE == Operation) && (rTYPE == Constant)) {
resultNode = MULTIPLICATION(cR, MULTIPLICATION(POWER(cL, SUBTRACTION(cR, CONSTANT(1))), dL));
} else if ((lTYPE == Constant) && ((rTYPE == Variable) || (rTYPE == Operation))) {
resultNode = MULTIPLICATION(MULTIPLICATION(POWER(cL, cR), LN(cR)), dR);
} else {
resultNode = MULTIPLICATION(POWER(cL, cR),
ADDITION(MULTIPLICATION(dR, LN(cL)),
MULTIPLICATION(cR, MULTIPLICATION(DIVISION(CONSTANT(1), cL), dL))));
}
END_DIFFERENTIATION("Every soviet kindergarten graduate knows");
return
resultNode;
}
}
}
}
}
bool Middleend::optimizeNeutralElementNodesRecursively(BinaryTree::Node **node)
{
if (*node == nullptr) return false;
bool optimizedLeftSubtree = optimizeNeutralElementNodesRecursively(&(*node)->left);
bool optimizedRightSubtree = optimizeNeutralElementNodesRecursively(&(*node)->right);
if ((*node)->type != Operation) return optimizedLeftSubtree || optimizedRightSubtree;
bool optimizedSubtree = false;
switch ((*node)->data) {
case Addition: {
if (isConstant((*node)->left, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "see \"Abstract Algebra: Theory and Applications\"");
optimizeRightNode(node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
if (isConstant((*node)->right, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "i dunno really why");
optimizeLeftNode(node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
break;
}
case Subtraction: {
if (isConstant((*node)->right, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "U WOT M8?");
optimizeLeftNode(node);
optimizedSubtree = true;
dumpTreeToLatex(*node);
END_EXPRESSION_CONVERSION(*node);
break;
}
break;
}
case Multiplication: {
if (isConstant((*node)->left, 1)) {
BEGIN_EXPRESSION_CONVERSION(*node, "many thanks to Evariste Galoi");
optimizeRightNode(node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
if (isConstant((*node)->right, 1)) {
BEGIN_EXPRESSION_CONVERSION(*node, "multiply by one son");
optimizeLeftNode(node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
break;
}
case Division:
case Power: {
if (isConstant((*node)->right, 1)) {
BEGIN_EXPRESSION_CONVERSION(*node, "we call node MATH power");
optimizeLeftNode(node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
}
}
return optimizedSubtree || optimizedLeftSubtree || optimizedRightSubtree;
}
bool Middleend::isConstant(const BinaryTree::Node *node, int constant)
{
return (node->type == Constant) && (node->data == constant);
}
bool Middleend::optimizeEvaluableNodesRecursively(BinaryTree::Node *node)
{
if (node == nullptr) return false;
bool optimizedLeftSubtree = optimizeEvaluableNodesRecursively(node->left);
bool optimizedRightSubtree = optimizeEvaluableNodesRecursively(node->right);
if ((node->type != Operation) || ((node->left == nullptr) && (node->right->type != Constant)) || (node->left->type != Constant) ||
(node->right->type != Constant)) {
return optimizedLeftSubtree || optimizedRightSubtree;
}
switch (node->data) {
case Addition: {
BEGIN_EXPRESSION_CONVERSION(node, "trust me, just check rick astley's song");
node->data = node->left->data + node->right->data;
break;
}
case Subtraction: {
BEGIN_EXPRESSION_CONVERSION(node, "look, i was gonna go easy on u");
node->data = node->left->data - node->right->data;
break;
}
case Multiplication: {
BEGIN_EXPRESSION_CONVERSION(node, "i'm beginning to feel like a rap god");
node->data = node->left->data * node->right->data;
break;
}
case Division: {
BEGIN_EXPRESSION_CONVERSION(node, "LMAO");
node->data = node->left->data / node->right->data;
break;
}
case Power: {
BEGIN_EXPRESSION_CONVERSION(node, "OK, im out of node shit");
node->data = std::pow(node->left->data, node->right->data);
break;
}
}
node->type = Constant;
std::free(node->left);
node->left = nullptr;
std::free(node->right);
node->right = nullptr;
END_EXPRESSION_CONVERSION(node);
return optimizedLeftSubtree || optimizedRightSubtree;
}
bool Middleend::optimizeSpecialConstantNodesRecursively(BinaryTree::Node **node)
{
if (*node == nullptr) return false;
bool optimizedLeftSubtree = optimizeSpecialConstantNodesRecursively(&(*node)->left);
bool optimizedRightSubtree = optimizeSpecialConstantNodesRecursively(&(*node)->right);
if ((*node)->type != Operation) return optimizedLeftSubtree || optimizedRightSubtree;
bool optimizedSubtree = false;
switch ((*node)->data) {
case Multiplication: {
if (isConstant((*node)->left, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "okay okay im literally baffled");
zeroNode(*node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
if (isConstant((*node)->right, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "zero divisor of the integers ring");
zeroNode(*node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
break;
}
case Division: {
if (isConstant((*node)->left, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "i cant keep myself serious");
zeroNode(*node);
optimizedSubtree = true;
END_EXPRESSION_CONVERSION(*node);
break;
}
}
case Power: {
if (isConstant((*node)->right, 0)) {
BEGIN_EXPRESSION_CONVERSION(*node, "you better tell me its worth it");
optimizeLeftNode(node);
}
}
}
return optimizedSubtree || optimizedLeftSubtree || optimizedRightSubtree;
}
void Middleend::optimizeTree(BinaryTree::Node **root)
{
while (true) {
bool optimized = false;
if (optimizeNeutralElementNodesRecursively(root)) optimized = true;
if (optimizeEvaluableNodesRecursively(*root)) optimized = true;
if (optimizeSpecialConstantNodesRecursively(root)) optimized = true;
if (!optimized) break;
}
}
void Middleend::optimizeLeftNode(BinaryTree::Node **node)
{
std::free((*node)->right);
(*node)->right = nullptr;
auto optimizedNode = (*node)->left;
std::free(*node);
*node = optimizedNode;
}
void Middleend::optimizeRightNode(BinaryTree::Node **node)
{
std::free((*node)->left);
(*node)->left = nullptr;
auto optimizedNode = (*node)->right;
std::free(*node);
*node = optimizedNode;
}
void Middleend::zeroNode(BinaryTree::Node *node)
{
BinaryTree::deleteRecursively(&node->left);
BinaryTree::deleteRecursively(&node->right);
node->type = Constant;
node->data = 0;
}
#endif