1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <libpspp/alloc.h>
27 #include <libpspp/assertion.h>
28 #include <data/calendar.h>
29 #include <data/data-in.h>
30 #include <libpspp/message.h>
33 #include <libpspp/misc.h>
34 #include <libpspp/pool.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
39 static union any_node *evaluate_tree (struct composite_node *,
41 static union any_node *optimize_tree (union any_node *, struct expression *);
44 expr_optimize (union any_node *node, struct expression *e)
46 int nonconst_cnt = 0; /* Number of nonconstant children. */
47 int sysmis_cnt = 0; /* Number of system-missing children. */
48 const struct operation *op;
49 struct composite_node *c;
52 /* We can't optimize an atom. */
53 if (is_atom (node->type))
56 /* Start by optimizing all the children. */
58 for (i = 0; i < c->arg_cnt; i++)
60 c->args[i] = expr_optimize (c->args[i], e);
61 if (c->args[i]->type == OP_number)
63 if (c->args[i]->number.n == SYSMIS)
67 if (!is_atom (c->args[i]->type))
71 op = &operations[c->type];
72 if (sysmis_cnt && (op->flags & OPF_ABSORB_MISS) == 0)
74 /* Most operations produce SYSMIS given any SYSMIS
76 assert (op->returns == OP_number || op->returns == OP_boolean);
77 if (op->returns == OP_number)
78 return expr_allocate_number (e, SYSMIS);
80 return expr_allocate_boolean (e, SYSMIS);
82 else if (!nonconst_cnt && (op->flags & OPF_NONOPTIMIZABLE) == 0)
84 /* Evaluate constant expressions. */
85 return evaluate_tree (&node->composite, e);
89 /* A few optimization possibilities are still left. */
90 return optimize_tree (node, e);
95 eq_double (union any_node *node, double n)
97 return node->type == OP_number && node->number.n == n;
100 static union any_node *
101 optimize_tree (union any_node *node, struct expression *e)
103 struct composite_node *n = &node->composite;
104 assert (is_composite (node->type));
106 /* If you add to these optimizations, please also add a
107 correctness test in tests/expressions/expressions.sh. */
109 /* x+0, x-0, 0+x => x. */
110 if ((n->type == OP_ADD || n->type == OP_SUB) && eq_double (n->args[1], 0.))
112 else if (n->type == OP_ADD && eq_double (n->args[0], 0.))
115 /* x*1, x/1, 1*x => x. */
116 else if ((n->type == OP_MUL || n->type == OP_DIV)
117 && eq_double (n->args[1], 1.))
119 else if (n->type == OP_MUL && eq_double (n->args[0], 1.))
122 /* 0*x, 0/x, x*0, MOD(0,x) => 0. */
123 else if (((n->type == OP_MUL || n->type == OP_DIV || n->type == OP_MOD_nn)
124 && eq_double (n->args[0], 0.))
125 || (n->type == OP_MUL && eq_double (n->args[1], 0.)))
126 return expr_allocate_number (e, 0.);
129 else if (n->type == OP_POW && eq_double (n->args[1], 1))
132 /* x**2 => SQUARE(x). */
133 else if (n->type == OP_POW && eq_double (n->args[1], 2))
134 return expr_allocate_unary (e, OP_SQUARE, n->args[0]);
136 /* Otherwise, nothing to do. */
141 static double get_number_arg (struct composite_node *, size_t arg_idx);
142 static double *get_number_args (struct composite_node *,
143 size_t arg_idx, size_t arg_cnt,
144 struct expression *);
145 static struct substring get_string_arg (struct composite_node *,
147 static struct substring *get_string_args (struct composite_node *,
148 size_t arg_idx, size_t arg_cnt,
149 struct expression *);
150 static const struct fmt_spec *get_format_arg (struct composite_node *,
153 static union any_node *
154 evaluate_tree (struct composite_node *node, struct expression *e)
158 #include "optimize.inc"
168 get_number_arg (struct composite_node *c, size_t arg_idx)
170 assert (arg_idx < c->arg_cnt);
171 assert (c->args[arg_idx]->type == OP_number
172 || c->args[arg_idx]->type == OP_boolean);
173 return c->args[arg_idx]->number.n;
177 get_number_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt,
178 struct expression *e)
183 d = pool_alloc (e->expr_pool, sizeof *d * arg_cnt);
184 for (i = 0; i < arg_cnt; i++)
185 d[i] = get_number_arg (c, i + arg_idx);
189 static struct substring
190 get_string_arg (struct composite_node *c, size_t arg_idx)
192 assert (arg_idx < c->arg_cnt);
193 assert (c->args[arg_idx]->type == OP_string);
194 return c->args[arg_idx]->string.s;
197 static struct substring *
198 get_string_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt,
199 struct expression *e)
204 s = pool_alloc (e->expr_pool, sizeof *s * arg_cnt);
205 for (i = 0; i < arg_cnt; i++)
206 s[i] = get_string_arg (c, i + arg_idx);
210 static const struct fmt_spec *
211 get_format_arg (struct composite_node *c, size_t arg_idx)
213 assert (arg_idx < c->arg_cnt);
214 assert (c->args[arg_idx]->type == OP_ni_format
215 || c->args[arg_idx]->type == OP_no_format);
216 return &c->args[arg_idx]->format.f;
219 /* Expression flattening. */
221 static union operation_data *allocate_aux (struct expression *,
223 static void flatten_node (union any_node *, struct expression *);
226 emit_operation (struct expression *e, operation_type type)
228 allocate_aux (e, OP_operation)->operation = type;
232 emit_number (struct expression *e, double n)
234 allocate_aux (e, OP_number)->number = n;
238 emit_string (struct expression *e, struct substring s)
240 allocate_aux (e, OP_string)->string = s;
244 emit_format (struct expression *e, const struct fmt_spec *f)
246 allocate_aux (e, OP_format)->format = pool_clone (e->expr_pool,
251 emit_variable (struct expression *e, struct variable *v)
253 allocate_aux (e, OP_variable)->variable = v;
257 emit_vector (struct expression *e, const struct vector *v)
259 allocate_aux (e, OP_vector)->vector = v;
263 emit_integer (struct expression *e, int i)
265 allocate_aux (e, OP_integer)->integer = i;
269 expr_flatten (union any_node *n, struct expression *e)
272 e->type = expr_node_returns (n);
273 emit_operation (e, (e->type == OP_string
274 ? OP_return_string : OP_return_number));
278 flatten_atom (union any_node *n, struct expression *e)
284 emit_operation (e, OP_number);
285 emit_number (e, n->number.n);
289 emit_operation (e, OP_string);
290 emit_string (e, n->string.s);
299 /* These are passed as aux data following the
309 flatten_composite (union any_node *n, struct expression *e)
311 const struct operation *op = &operations[n->type];
314 for (i = 0; i < n->composite.arg_cnt; i++)
315 flatten_node (n->composite.args[i], e);
317 if (n->type != OP_BOOLEAN_TO_NUM)
318 emit_operation (e, n->type);
320 for (i = 0; i < n->composite.arg_cnt; i++)
322 union any_node *arg = n->composite.args[i];
327 emit_variable (e, arg->variable.v);
331 emit_vector (e, arg->vector.v);
336 emit_format (e, &arg->format.f);
340 emit_integer (e, arg->integer.i);
349 if (op->flags & OPF_ARRAY_OPERAND)
350 emit_integer (e, n->composite.arg_cnt - op->arg_cnt + 1);
351 if (op->flags & OPF_MIN_VALID)
352 emit_integer (e, n->composite.min_valid);
356 flatten_node (union any_node *n, struct expression *e)
358 assert (is_operation (n->type));
360 if (is_atom (n->type))
362 else if (is_composite (n->type))
363 flatten_composite (n, e);
368 static union operation_data *
369 allocate_aux (struct expression *e, operation_type type)
371 if (e->op_cnt >= e->op_cap)
373 e->op_cap = (e->op_cap + 8) * 3 / 2;
374 e->ops = pool_realloc (e->expr_pool, e->ops, sizeof *e->ops * e->op_cap);
375 e->op_types = pool_realloc (e->expr_pool, e->op_types,
376 sizeof *e->op_types * e->op_cap);
379 e->op_types[e->op_cnt] = type;
380 return &e->ops[e->op_cnt++];