-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimizer.c
170 lines (166 loc) · 4.81 KB
/
optimizer.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
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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "optimizer.h"
#include "tokenizer.h"
tree *compute_16bit(token *operator, uint16_t a, uint16_t b) {
uint16_t output;
switch(operator->type) {
case TOK_DIVIDE:
output = a / b;
break;
case TOK_MOD:
output = a % b;
break;
case TOK_BITWISE_OR:
output = a | b;
break;
case TOK_XOR:
output = a ^ b;
break;
case TOK_LSH:
output = a << b;
break;
case TOK_RSH:
output = a >> b;
break;
case TOK_AND:
output = a && b;
break;
case TOK_OR:
output = a || b;
break;
case TOK_EQUAL_TO:
output = a == b;
break;
case TOK_NOT_EQUAL:
output = a != b;
break;
case TOK_LESS:
output = a < b;
break;
case TOK_MORE:
output = a > b;
break;
case TOK_LESS_EQUAL:
output = a <= b;
break;
case TOK_MORE_EQUAL:
output = a >= b;
break;
case TOK_ADD:
output = a + b;
break;
case TOK_SUBTRACT:
output = a - b;
break;
case TOK_MULTIPLY:
output = a * b;
break;
case TOK_BITWISE_AND:
output = a & b;
break;
}
tree *out = create_tree(TREETYPE_INTEGER);
out->data.int_value = output;
return out;
}
tree *compute_8bit(token *operator, uint8_t a, uint8_t b) {
uint8_t output;
switch(operator->type) {
case TOK_DIVIDE:
output = a / b;
break;
case TOK_MOD:
output = a % b;
break;
case TOK_BITWISE_OR:
output = a | b;
break;
case TOK_XOR:
output = a ^ b;
break;
case TOK_LSH:
output = a << b;
break;
case TOK_RSH:
output = a >> b;
break;
case TOK_AND:
output = a && b;
break;
case TOK_OR:
output = a || b;
break;
case TOK_EQUAL_TO:
output = a == b;
break;
case TOK_NOT_EQUAL:
output = a != b;
break;
case TOK_LESS:
output = a < b;
break;
case TOK_MORE:
output = a > b;
break;
case TOK_LESS_EQUAL:
output = a <= b;
break;
case TOK_MORE_EQUAL:
output = a >= b;
break;
case TOK_ADD:
output = a + b;
break;
case TOK_SUBTRACT:
output = a - b;
break;
case TOK_MULTIPLY:
output = a * b;
break;
case TOK_BITWISE_AND:
output = a & b;
break;
}
tree *out = create_tree(TREETYPE_CHAR);
out->data.int_value = output;
return out;
}
tree *optimize_expression(tree *in) {
if(in->type == TREETYPE_OPERATOR) {
if(is_single_argument(in->data.tok->type)) {
optimize_expression(in->left);
return in;
} else {
in->left = optimize_expression(in->left);
in->right = optimize_expression(in->right);
int a = in->left->type;
int b = in->right->type;
if((a == TREETYPE_INTEGER || a == TREETYPE_CHAR) && (b == TREETYPE_INTEGER || b == TREETYPE_CHAR)) {
if(a == TREETYPE_INTEGER || b == TREETYPE_INTEGER) {
tree *out = compute_16bit(in->data.tok, in->left->data.int_value, in->right->data.int_value);
if(out == NULL) {
return in;
}
printf("optimizing %d %s %d = %d\n", in->left->data.int_value, get_string_from_toktype(in->data.tok->type), in->right->data.int_value, out->data.int_value);
return out;
} else {
tree *out = compute_8bit(in->data.tok, in->left->data.int_value, in->right->data.int_value);
if(out == NULL) {
return in;
}
printf("optimizing %d %s %d = %d\n", in->left->data.int_value, get_string_from_toktype(in->data.tok->type), in->right->data.int_value, out->data.int_value);
return out;
}
} else {
return in;
}
}
} else if(in->type == TREETYPE_VARIABLE || in->type == TREETYPE_INTEGER || in->type == TREETYPE_STRING || in->type == TREETYPE_CHAR) {
return in;
} else {
printf("weird tree type: %d\n", in->type);
exit(1);
}
}