1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <libpspp/alloc.h>
24 #include <libpspp/assertion.h>
25 #include <data/calendar.h>
26 #include <data/data-in.h>
27 #include <libpspp/message.h>
30 #include <libpspp/misc.h>
31 #include <libpspp/pool.h>
33 #include <libpspp/str.h>
34 #include <data/variable.h>
36 static union any_node *evaluate_tree (struct composite_node *,
38 static union any_node *optimize_tree (union any_node *, struct expression *);
41 expr_optimize (union any_node *node, struct expression *e)
43 int nonconst_cnt = 0; /* Number of nonconstant children. */
44 int sysmis_cnt = 0; /* Number of system-missing children. */
45 const struct operation *op;
46 struct composite_node *c;
49 /* We can't optimize an atom. */
50 if (is_atom (node->type))
53 /* Start by optimizing all the children. */
55 for (i = 0; i < c->arg_cnt; i++)
57 c->args[i] = expr_optimize (c->args[i], e);
58 if (c->args[i]->type == OP_number)
60 if (c->args[i]->number.n == SYSMIS)
64 if (!is_atom (c->args[i]->type))
68 op = &operations[c->type];
69 if (sysmis_cnt && (op->flags & OPF_ABSORB_MISS) == 0)
71 /* Most operations produce SYSMIS given any SYSMIS
73 assert (op->returns == OP_number || op->returns == OP_boolean);
74 if (op->returns == OP_number)
75 return expr_allocate_number (e, SYSMIS);
77 return expr_allocate_boolean (e, SYSMIS);
79 else if (!nonconst_cnt && (op->flags & OPF_NONOPTIMIZABLE) == 0)
81 /* Evaluate constant expressions. */
82 return evaluate_tree (&node->composite, e);
86 /* A few optimization possibilities are still left. */
87 return optimize_tree (node, e);
92 eq_double (union any_node *node, double n)
94 return node->type == OP_number && node->number.n == n;
97 static union any_node *
98 optimize_tree (union any_node *node, struct expression *e)
100 struct composite_node *n = &node->composite;
101 assert (is_composite (node->type));
103 /* If you add to these optimizations, please also add a
104 correctness test in tests/expressions/expressions.sh. */
106 /* x+0, x-0, 0+x => x. */
107 if ((n->type == OP_ADD || n->type == OP_SUB) && eq_double (n->args[1], 0.))
109 else if (n->type == OP_ADD && eq_double (n->args[0], 0.))
112 /* x*1, x/1, 1*x => x. */
113 else if ((n->type == OP_MUL || n->type == OP_DIV)
114 && eq_double (n->args[1], 1.))
116 else if (n->type == OP_MUL && eq_double (n->args[0], 1.))
119 /* 0*x, 0/x, x*0, MOD(0,x) => 0. */
120 else if (((n->type == OP_MUL || n->type == OP_DIV || n->type == OP_MOD_nn)
121 && eq_double (n->args[0], 0.))
122 || (n->type == OP_MUL && eq_double (n->args[1], 0.)))
123 return expr_allocate_number (e, 0.);
126 else if (n->type == OP_POW && eq_double (n->args[1], 1))
129 /* x**2 => SQUARE(x). */
130 else if (n->type == OP_POW && eq_double (n->args[1], 2))
131 return expr_allocate_unary (e, OP_SQUARE, n->args[0]);
133 /* Otherwise, nothing to do. */
138 static double get_number_arg (struct composite_node *, size_t arg_idx);
139 static double *get_number_args (struct composite_node *,
140 size_t arg_idx, size_t arg_cnt,
141 struct expression *);
142 static struct substring get_string_arg (struct composite_node *,
144 static struct substring *get_string_args (struct composite_node *,
145 size_t arg_idx, size_t arg_cnt,
146 struct expression *);
147 static const struct fmt_spec *get_format_arg (struct composite_node *,
150 static union any_node *
151 evaluate_tree (struct composite_node *node, struct expression *e)
155 #include "optimize.inc"
165 get_number_arg (struct composite_node *c, size_t arg_idx)
167 assert (arg_idx < c->arg_cnt);
168 assert (c->args[arg_idx]->type == OP_number
169 || c->args[arg_idx]->type == OP_boolean);
170 return c->args[arg_idx]->number.n;
174 get_number_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt,
175 struct expression *e)
180 d = pool_alloc (e->expr_pool, sizeof *d * arg_cnt);
181 for (i = 0; i < arg_cnt; i++)
182 d[i] = get_number_arg (c, i + arg_idx);
186 static struct substring
187 get_string_arg (struct composite_node *c, size_t arg_idx)
189 assert (arg_idx < c->arg_cnt);
190 assert (c->args[arg_idx]->type == OP_string);
191 return c->args[arg_idx]->string.s;
194 static struct substring *
195 get_string_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt,
196 struct expression *e)
201 s = pool_alloc (e->expr_pool, sizeof *s * arg_cnt);
202 for (i = 0; i < arg_cnt; i++)
203 s[i] = get_string_arg (c, i + arg_idx);
207 static const struct fmt_spec *
208 get_format_arg (struct composite_node *c, size_t arg_idx)
210 assert (arg_idx < c->arg_cnt);
211 assert (c->args[arg_idx]->type == OP_ni_format
212 || c->args[arg_idx]->type == OP_no_format);
213 return &c->args[arg_idx]->format.f;
216 /* Expression flattening. */
218 static union operation_data *allocate_aux (struct expression *,
220 static void flatten_node (union any_node *, struct expression *);
223 emit_operation (struct expression *e, operation_type type)
225 allocate_aux (e, OP_operation)->operation = type;
229 emit_number (struct expression *e, double n)
231 allocate_aux (e, OP_number)->number = n;
235 emit_string (struct expression *e, struct substring s)
237 allocate_aux (e, OP_string)->string = s;
241 emit_format (struct expression *e, const struct fmt_spec *f)
243 allocate_aux (e, OP_format)->format = pool_clone (e->expr_pool,
248 emit_variable (struct expression *e, const struct variable *v)
250 allocate_aux (e, OP_variable)->variable = v;
254 emit_vector (struct expression *e, const struct vector *v)
256 allocate_aux (e, OP_vector)->vector = v;
260 emit_integer (struct expression *e, int i)
262 allocate_aux (e, OP_integer)->integer = i;
266 expr_flatten (union any_node *n, struct expression *e)
269 e->type = expr_node_returns (n);
270 emit_operation (e, (e->type == OP_string
271 ? OP_return_string : OP_return_number));
275 flatten_atom (union any_node *n, struct expression *e)
281 emit_operation (e, OP_number);
282 emit_number (e, n->number.n);
286 emit_operation (e, OP_string);
287 emit_string (e, n->string.s);
296 /* These are passed as aux data following the
306 flatten_composite (union any_node *n, struct expression *e)
308 const struct operation *op = &operations[n->type];
311 for (i = 0; i < n->composite.arg_cnt; i++)
312 flatten_node (n->composite.args[i], e);
314 if (n->type != OP_BOOLEAN_TO_NUM)
315 emit_operation (e, n->type);
317 for (i = 0; i < n->composite.arg_cnt; i++)
319 union any_node *arg = n->composite.args[i];
324 emit_variable (e, arg->variable.v);
328 emit_vector (e, arg->vector.v);
333 emit_format (e, &arg->format.f);
337 emit_integer (e, arg->integer.i);
346 if (op->flags & OPF_ARRAY_OPERAND)
347 emit_integer (e, n->composite.arg_cnt - op->arg_cnt + 1);
348 if (op->flags & OPF_MIN_VALID)
349 emit_integer (e, n->composite.min_valid);
353 flatten_node (union any_node *n, struct expression *e)
355 assert (is_operation (n->type));
357 if (is_atom (n->type))
359 else if (is_composite (n->type))
360 flatten_composite (n, e);
365 static union operation_data *
366 allocate_aux (struct expression *e, operation_type type)
368 if (e->op_cnt >= e->op_cap)
370 e->op_cap = (e->op_cap + 8) * 3 / 2;
371 e->ops = pool_realloc (e->expr_pool, e->ops, sizeof *e->ops * e->op_cap);
372 e->op_types = pool_realloc (e->expr_pool, e->op_types,
373 sizeof *e->op_types * e->op_cap);
376 e->op_types[e->op_cnt] = type;
377 return &e->ops[e->op_cnt++];