pn2adg: handle dynamic control variables in generated AST
[ppn.git] / condition_tree.h
blob23ba180d6159db6e2be0c80cffc8fc1a25e320a6
1 #include "suif_inc.h"
3 enum condition_tree_type {
4 ctt_invalid,
5 ctt_and,
6 ctt_or,
7 ctt_ge,
8 ctt_eq,
9 ctt_cst,
10 ctt_sym,
11 ctt_neg,
12 ctt_add,
13 ctt_sub,
14 ctt_mul,
15 ctt_rem,
16 ctt_max,
17 ctt_min,
18 ctt_ceild,
19 ctt_floord,
20 ctt_lod,
23 struct condition_tree {
24 condition_tree_type type;
26 int val;
27 var_sym *var;
28 operand op;
29 condition_tree *left;
30 condition_tree *right;
32 condition_tree(condition_tree_type type) :
33 type(type), val(-1), var(NULL), left(NULL), right(NULL) {}
34 condition_tree(int val) :
35 type(ctt_cst), val(val), var(NULL), left(NULL), right(NULL) {}
36 condition_tree(var_sym *var) :
37 type(ctt_sym), val(-1), var(var), left(NULL), right(NULL) {}
38 condition_tree(const operand &op) :
39 type(ctt_lod), val(-1), var(NULL), op(op), left(NULL), right(NULL) {}
40 condition_tree(condition_tree_type type, condition_tree *left) :
41 type(type), val(-1), var(NULL),
42 left(left), right(NULL) {
43 if (!left || left->type == ctt_invalid) {
44 if (left)
45 delete left;
46 this->left = NULL;
47 this->type = ctt_invalid;
48 } else if (type == ctt_neg && left->type == ctt_cst) {
49 this->val *= left->val;
50 this->left = NULL;
51 delete left;
52 this->type = ctt_cst;
55 condition_tree(condition_tree_type type,
56 condition_tree *left, condition_tree *right) :
57 type(type), val(-1), var(NULL),
58 left(left), right(right) {
59 if (!left || left->type == ctt_invalid ||
60 !right || right->type == ctt_invalid) {
61 if (left)
62 delete left;
63 if (right)
64 delete right;
65 this->left = NULL;
66 this->right = NULL;
67 this->type = ctt_invalid;
68 } else if (type == ctt_sub) {
69 this->type = ctt_add;
70 this->right = new condition_tree(ctt_neg, right);
71 } else if (type == ctt_mul &&
72 left->type == ctt_cst && right->type == ctt_cst) {
73 this->val = left->val * right->val;
74 delete left;
75 delete right;
76 this->left = NULL;
77 this->right = NULL;
78 this->type = ctt_cst;
79 } else if (type == ctt_mul &&
80 left->type == ctt_cst && right->type == ctt_neg) {
81 condition_tree *tmp = right;
82 left->val *= -1;
83 this->right = tmp->left;
84 tmp->left = NULL;
85 delete tmp;
86 } else if (type == ctt_mul &&
87 left->type == ctt_cst && right->type == ctt_add) {
88 this->left = new condition_tree(ctt_mul,
89 new condition_tree(left->val),
90 right->left);
91 this->right = new condition_tree(ctt_mul,
92 left, right->right);
93 right->left = NULL;
94 right->right = NULL;
95 delete right;
96 this->type = ctt_add;
97 } else if (type == ctt_mul &&
98 left->type == ctt_cst && right->type == ctt_mul) {
99 this->left = new condition_tree(ctt_mul,
100 left, right->left);
101 this->right = right->right;
102 right->left = NULL;
103 right->right = NULL;
104 delete right;
107 condition_tree(condition_tree_type type,
108 int val, var_sym *var,
109 condition_tree *left, condition_tree *right) :
110 type(type), val(val), var(var),
111 left(left), right(right) {
114 condition_tree *dup() {
115 return new condition_tree(type, val, var,
116 left ? left->dup() : NULL,
117 right ? right->dup() : NULL);
120 void dump(FILE *out = stderr, int indent = 0);
121 void eliminate_minmax();
123 ~condition_tree() {
124 if (left)
125 delete left;
126 if (right)
127 delete right;
131 condition_tree *construct_condition_tree(operand op, int verbose);
132 condition_tree *construct_condition_tree_expr(instruction *ins, int verbose);
133 condition_tree *construct_condition_tree(instruction *ins, int verbose);