forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt.c
101 lines (88 loc) · 2.32 KB
/
opt.c
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
#include "defs.h"
#include "data.h"
#include "decl.h"
// AST Tree Optimisation Code
// Copyright (c) 2019 Warren Toomey, GPL3
// Fold an AST tree with a binary operator
// and two A_INTLIT children. Return either
// the original tree or a new leaf node.
static struct ASTnode *fold2(struct ASTnode *n) {
int val, leftval, rightval;
// Get the values from each child
leftval = n->left->a_intvalue;
rightval = n->right->a_intvalue;
// Perform some of the binary operations.
// For any AST op we can't do, return
// the original tree.
switch (n->op) {
case A_ADD:
val = leftval + rightval;
break;
case A_SUBTRACT:
val = leftval - rightval;
break;
case A_MULTIPLY:
val = leftval * rightval;
break;
case A_DIVIDE:
// Don't try to divide by zero.
if (rightval == 0)
return (n);
val = leftval / rightval;
break;
default:
return (n);
}
// Return a leaf node with the new value
return (mkastleaf(A_INTLIT, n->type, NULL, NULL, val));
}
// Fold an AST tree with a unary operator
// and one INTLIT children. Return either
// the original tree or a new leaf node.
static struct ASTnode *fold1(struct ASTnode *n) {
int val;
// Get the child value. Do the
// operation if recognised.
// Return the new leaf node.
val = n->left->a_intvalue;
switch (n->op) {
case A_WIDEN:
break;
case A_INVERT:
val = ~val;
break;
case A_LOGNOT:
val = !val;
break;
default:
return (n);
}
// Return a leaf node with the new value
return (mkastleaf(A_INTLIT, n->type, NULL, NULL, val));
}
// Attempt to do constant folding on
// the AST tree with the root node n
static struct ASTnode *fold(struct ASTnode *n) {
if (n == NULL)
return (NULL);
// Fold on the left child, then
// do the same on the right child
n->left = fold(n->left);
n->right = fold(n->right);
// If both children are A_INTLITs, do a fold2()
if (n->left && n->left->op == A_INTLIT) {
if (n->right && n->right->op == A_INTLIT)
n = fold2(n);
else
// If only the left is A_INTLIT, do a fold1()
n = fold1(n);
}
// Return the possibly modified tree
return (n);
}
// Optimise an AST tree by
// constant folding in all sub-trees
struct ASTnode *optimise(struct ASTnode *n) {
n = fold(n);
return (n);
}