X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fexpressions%2Foptimize.c;h=b0abd946c57b9868b6346f77d45d68cf5a45ccc3;hb=a1efcf97ca2f75f4be6a0389ff2372c03ed2d4e1;hp=b280b6d737485527d92e5fde4b6d48af97d63e99;hpb=dcf9b154cbcaa35c3d8459a201b77eec8bcb30bd;p=pspp-builds.git diff --git a/src/language/expressions/optimize.c b/src/language/expressions/optimize.c index b280b6d7..b0abd946 100644 --- a/src/language/expressions/optimize.c +++ b/src/language/expressions/optimize.c @@ -1,21 +1,18 @@ -/* PSPP - computes sample statistics. +/* PSPP - a program for statistical analysis. Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include #include "private.h" @@ -23,17 +20,19 @@ #include #include #include -#include "alloc.h" -#include "calendar.h" -#include "data-in.h" -#include "message.h" +#include +#include +#include +#include #include "evaluate.h" #include "helpers.h" -#include "misc.h" -#include "pool.h" +#include +#include #include "public.h" -#include "str.h" -#include "variable.h" +#include +#include + +#include "xalloc.h" static union any_node *evaluate_tree (struct composite_node *, struct expression *); @@ -44,7 +43,7 @@ expr_optimize (union any_node *node, struct expression *e) { int nonconst_cnt = 0; /* Number of nonconstant children. */ int sysmis_cnt = 0; /* Number of system-missing children. */ - struct operation *op; + const struct operation *op; struct composite_node *c; int i; @@ -78,20 +77,20 @@ expr_optimize (union any_node *node, struct expression *e) else return expr_allocate_boolean (e, SYSMIS); } - else if (!nonconst_cnt && (op->flags & OPF_NONOPTIMIZABLE) == 0) + else if (!nonconst_cnt && (op->flags & OPF_NONOPTIMIZABLE) == 0) { /* Evaluate constant expressions. */ return evaluate_tree (&node->composite, e); } - else + else { /* A few optimization possibilities are still left. */ - return optimize_tree (node, e); + return optimize_tree (node, e); } } static int -eq_double (union any_node *node, double n) +eq_double (union any_node *node, double n) { return node->type == OP_number && node->number.n == n; } @@ -108,7 +107,7 @@ optimize_tree (union any_node *node, struct expression *e) /* x+0, x-0, 0+x => x. */ if ((n->type == OP_ADD || n->type == OP_SUB) && eq_double (n->args[1], 0.)) return n->args[0]; - else if (n->type == OP_ADD && eq_double (n->args[0], 0.)) + else if (n->type == OP_ADD && eq_double (n->args[0], 0.)) return n->args[1]; /* x*1, x/1, 1*x => x. */ @@ -117,7 +116,7 @@ optimize_tree (union any_node *node, struct expression *e) return n->args[0]; else if (n->type == OP_MUL && eq_double (n->args[0], 1.)) return n->args[1]; - + /* 0*x, 0/x, x*0, MOD(0,x) => 0. */ else if (((n->type == OP_MUL || n->type == OP_DIV || n->type == OP_MOD_nn) && eq_double (n->args[0], 0.)) @@ -127,7 +126,7 @@ optimize_tree (union any_node *node, struct expression *e) /* x**1 => x. */ else if (n->type == OP_POW && eq_double (n->args[1], 1)) return n->args[0]; - + /* x**2 => SQUARE(x). */ else if (n->type == OP_POW && eq_double (n->args[1], 2)) return expr_allocate_unary (e, OP_SQUARE, n->args[0]); @@ -141,9 +140,9 @@ static double get_number_arg (struct composite_node *, size_t arg_idx); static double *get_number_args (struct composite_node *, size_t arg_idx, size_t arg_cnt, struct expression *); -static struct fixed_string get_string_arg (struct composite_node *, +static struct substring get_string_arg (struct composite_node *, size_t arg_idx); -static struct fixed_string *get_string_args (struct composite_node *, +static struct substring *get_string_args (struct composite_node *, size_t arg_idx, size_t arg_cnt, struct expression *); static const struct fmt_spec *get_format_arg (struct composite_node *, @@ -152,21 +151,19 @@ static const struct fmt_spec *get_format_arg (struct composite_node *, static union any_node * evaluate_tree (struct composite_node *node, struct expression *e) { - switch (node->type) + switch (node->type) { #include "optimize.inc" default: - assert (0); + NOT_REACHED (); } - /* Not reached. */ - assert (0); - abort (); + NOT_REACHED (); } static double -get_number_arg (struct composite_node *c, size_t arg_idx) +get_number_arg (struct composite_node *c, size_t arg_idx) { assert (arg_idx < c->arg_cnt); assert (c->args[arg_idx]->type == OP_number @@ -176,7 +173,7 @@ get_number_arg (struct composite_node *c, size_t arg_idx) static double * get_number_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt, - struct expression *e) + struct expression *e) { double *d; size_t i; @@ -187,19 +184,19 @@ get_number_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt, return d; } -static struct fixed_string -get_string_arg (struct composite_node *c, size_t arg_idx) +static struct substring +get_string_arg (struct composite_node *c, size_t arg_idx) { assert (arg_idx < c->arg_cnt); assert (c->args[arg_idx]->type == OP_string); return c->args[arg_idx]->string.s; } -static struct fixed_string * +static struct substring * get_string_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt, - struct expression *e) + struct expression *e) { - struct fixed_string *s; + struct substring *s; size_t i; s = pool_alloc (e->expr_pool, sizeof *s * arg_cnt); @@ -209,7 +206,7 @@ get_string_args (struct composite_node *c, size_t arg_idx, size_t arg_cnt, } static const struct fmt_spec * -get_format_arg (struct composite_node *c, size_t arg_idx) +get_format_arg (struct composite_node *c, size_t arg_idx) { assert (arg_idx < c->arg_cnt); assert (c->args[arg_idx]->type == OP_ni_format @@ -224,50 +221,50 @@ static union operation_data *allocate_aux (struct expression *, static void flatten_node (union any_node *, struct expression *); static void -emit_operation (struct expression *e, operation_type type) +emit_operation (struct expression *e, operation_type type) { allocate_aux (e, OP_operation)->operation = type; } static void -emit_number (struct expression *e, double n) +emit_number (struct expression *e, double n) { allocate_aux (e, OP_number)->number = n; } static void -emit_string (struct expression *e, struct fixed_string s) +emit_string (struct expression *e, struct substring s) { allocate_aux (e, OP_string)->string = s; } static void -emit_format (struct expression *e, const struct fmt_spec *f) +emit_format (struct expression *e, const struct fmt_spec *f) { allocate_aux (e, OP_format)->format = pool_clone (e->expr_pool, f, sizeof *f); } static void -emit_variable (struct expression *e, struct variable *v) +emit_variable (struct expression *e, const struct variable *v) { allocate_aux (e, OP_variable)->variable = v; } static void -emit_vector (struct expression *e, const struct vector *v) +emit_vector (struct expression *e, const struct vector *v) { allocate_aux (e, OP_vector)->vector = v; } static void -emit_integer (struct expression *e, int i) +emit_integer (struct expression *e, int i) { allocate_aux (e, OP_integer)->integer = i; } -void -expr_flatten (union any_node *n, struct expression *e) +void +expr_flatten (union any_node *n, struct expression *e) { flatten_node (n, e); e->type = expr_node_returns (n); @@ -278,7 +275,7 @@ expr_flatten (union any_node *n, struct expression *e) static void flatten_atom (union any_node *n, struct expression *e) { - switch (n->type) + switch (n->type) { case OP_number: case OP_boolean: @@ -302,16 +299,16 @@ flatten_atom (union any_node *n, struct expression *e) break; default: - abort (); + NOT_REACHED (); } } static void flatten_composite (union any_node *n, struct expression *e) { - struct operation *op = &operations[n->type]; + const struct operation *op = &operations[n->type]; size_t i; - + for (i = 0; i < n->composite.arg_cnt; i++) flatten_node (n->composite.args[i], e); @@ -321,7 +318,7 @@ flatten_composite (union any_node *n, struct expression *e) for (i = 0; i < n->composite.arg_cnt; i++) { union any_node *arg = n->composite.args[i]; - switch (arg->type) + switch (arg->type) { case OP_num_var: case OP_str_var: @@ -340,7 +337,7 @@ flatten_composite (union any_node *n, struct expression *e) case OP_pos_int: emit_integer (e, arg->integer.i); break; - + default: /* Nothing to do. */ break; @@ -362,14 +359,14 @@ flatten_node (union any_node *n, struct expression *e) flatten_atom (n, e); else if (is_composite (n->type)) flatten_composite (n, e); - else - abort (); + else + NOT_REACHED (); } static union operation_data * -allocate_aux (struct expression *e, operation_type type) +allocate_aux (struct expression *e, operation_type type) { - if (e->op_cnt >= e->op_cap) + if (e->op_cnt >= e->op_cap) { e->op_cap = (e->op_cap + 8) * 3 / 2; e->ops = pool_realloc (e->expr_pool, e->ops, sizeof *e->ops * e->op_cap);