PIPS
expression.c
Go to the documentation of this file.
1 /*
2 
3  $Id: expression.c 23660 2023-07-07 13:03:00Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27  /*
28  * Functions to prettyprint expressions
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 
35 #include "linear.h"
36 #include "genC.h"
37 #include "misc.h"
38 #include "ri.h"
39 
40 #include "text.h"
41 #include "text-util.h" // words stuff
42 
43 #include "ri-util.h"
44 #include "workspace-util.h"
45 #include "prettyprint.h"
46 
48 {
49  list pdl = NIL;
51  gen_free_list(pdl);
52 }
53 
54 /* no file descriptor is passed to make is easier to use in a debugging
55  stage.
56  Do not make macros of those printing functions */
57 
59 {
61  (void) fprintf(stderr,"EXPRESSION UNDEFINED\n");
62  // For debugging with gdb, dynamic type checking
64  (void) fprintf(stderr,"Arg. \"e\"is not an expression.\n");
65  else {
66  normalized n;
67  (void) fprintf(stderr,"syntax = ");
69  (void) fprintf(stderr,"\nnormalized = ");
72  else
73  (void) fprintf(stderr,"NORMALIZED UNDEFINED\n");
74  }
75 }
76 
78  list pdl = NIL;
79  list l = words_expression(e, &pdl) ;
80  string out = words_to_string(l);
81  FOREACH(STRING,w,l) free(w);
82  gen_free_list(l);
83  gen_free_list(pdl);
84  return out;
85 }
86 
88  list pdl = NIL;
89  list l = words_reference(r,&pdl) ;
90  gen_free_list(pdl);
91  string out = words_to_string(l);
92  FOREACH(STRING,w,l) free(w);
93  gen_free_list(l);
94  return out;
95 }
96 
97 
99 {
100 
101  MAP(EXPRESSION, e , {
102  print_expression(e);
103  },
104  le);
105 
106 }
107 
109 {
110 
111  MAP(EXPRESSION, e , {
113  if(!ENDP(CDR(le))) {
114  (void) fprintf(stderr, ", ");
115  }
116  },
117  le);
118 
119 }
120 
122 {
123  list pdl = NIL;
124  print_words(stderr,words_syntax(s, &pdl));
125  gen_free_list(pdl);
126 }
127 
128 void fprint_reference(FILE * fd, reference r)
129 {
130  if(reference_undefined_p(r))
131  fprintf(fd, "reference undefined\n");
132  // For debugging with gdb, dynamic type checking
134  fprintf(fd, "Not a Newgen \"reference\" object\n");
135  else {
136  list pdl = NIL;
137  print_words(fd,words_reference(r, &pdl));
138  gen_free_list(pdl);
139  }
140 }
141 
143 {
144  fprint_reference(stderr, r);
145 }
146 
148 {
149  if(ENDP(lr))
150  fputs("NIL", stderr);
151  else
152  MAPL(cr,
153  {
154  reference r = REFERENCE(CAR(cr));
155  entity e = reference_variable(r);
156  (void) fprintf(stderr,"%s, ", entity_local_name(e));
157  },
158  lr);
159 
160  (void) putc('\n', stderr);
161 }
162 
164 {
166 }
167 
169 {
170  if(normalized_complex_p(n))
171  (void) fprintf(stderr,"COMPLEX\n");
172  else
173  /* should be replaced by a call to expression_fprint() if it's
174  ever added to linear library */
176 }
177 
178 /* call maxima to simplify an expression
179  * prefer simplify_expression !*/
180 bool maxima_simplify(expression *presult) {
181  bool success = true;
182  expression result = *presult;
183  /* try to call maxima to simplify this expression */
184  if(!expression_undefined_p(result) ) {
185  list pdl = NIL;
186  list w = words_expression(result,&pdl);
187  gen_free_list(pdl);
188  string str = words_to_string(w);
189  gen_free_list(w);
190  char * cmd;
191  asprintf(&cmd,"maxima -q --batch-string \"string(fullratsimp(%s));\"\n",str);
192  free(str);
193  FILE* pout = popen(cmd,"r");
194  if(pout) {
195  /* strip out banner */
196  fgetc(pout);fgetc(pout);
197  /* look for first % */
198  while(!feof(pout) && fgetc(pout)!='%');
199  if(!feof(pout)) {
200  /* skip the three next chars */
201  fgetc(pout);fgetc(pout);fgetc(pout);
202  /* parse the output */
203  char bufline[strlen(cmd)];
204  if(fscanf(pout," %s\n",&bufline[0]) == 1 ) {
207  free_expression(result);
208  *presult=exp;
209  }
210  else
211  success= false;
212  }
213  }
214  else
215  success= false;
216  pclose(pout);
217  }
218  else
219  success= false;
220  free(cmd);
221  }
222  return success;
223 }
224 
225 /* void fprint_list_of_exp(FILE *fp, list exp_l): prints in the file "fp"
226  * the list of expression "exp_l". We separate the expressions with a colon
227  * (","). We do not end the print with a line feed.
228  */
229 void fprint_list_of_exp(FILE * fp, list exp_l)
230 {
231  list aux_l;
232  expression exp;
233  list pdl = NIL;
234 
235  for(aux_l = exp_l; aux_l != NIL; aux_l = CDR(aux_l))
236  {
237  exp = EXPRESSION(CAR(aux_l));
238  fprintf(fp,"%s", words_to_string(words_expression(exp, &pdl)));
239  if(CDR(aux_l) != NIL)
240  fprintf(fp,",");
241  }
242  gen_free_list(pdl);
243 }
void free_expression(expression p)
Definition: ri.c:853
static FILE * out
Definition: alias_check.c:128
void vect_debug(Pvecteur v)
constraint.c
Definition: constraint.c:43
expression string_to_expression(const char *s, entity module)
Functions using simultaneously pipsdbm, which is based on strings, and ri-util, which contains basic ...
Definition: expressions.c:50
#define STRING(x)
Definition: genC.h:87
void free(void *)
bool success
Definition: gpips-local.h:59
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
#define asprintf
Definition: misc-local.h:225
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
void print_references(list rl)
Definition: expression.c:163
void print_expressions(list le)
Definition: expression.c:98
void fprint_list_of_exp(FILE *fp, list exp_l)
void fprint_list_of_exp(FILE *fp, list exp_l): prints in the file "fp" the list of expression "exp_l"...
Definition: expression.c:229
void print_normalized(normalized n)
Definition: expression.c:168
void print_syntax_expressions(list le)
Definition: expression.c:108
bool maxima_simplify(expression *presult)
call maxima to simplify an expression prefer simplify_expression !
Definition: expression.c:180
void fprint_reference(FILE *fd, reference r)
Definition: expression.c:128
void print_reference_list(list lr)
Definition: expression.c:147
string reference_to_string(reference r)
Definition: expression.c:87
void fprint_expression(FILE *f, expression e)
expression.c
Definition: expression.c:47
void print_syntax(syntax s)
Definition: expression.c:121
void print_expression(expression e)
no file descriptor is passed to make is easier to use in a debugging stage.
Definition: expression.c:58
string expression_to_string(expression e)
Definition: expression.c:77
void print_reference(reference r)
Definition: expression.c:142
list words_syntax(syntax obj, list *ppdl)
exported for expression.c
Definition: misc.c:2623
list words_expression(expression obj, list *ppdl)
This one is exported.
Definition: misc.c:2611
list words_reference(reference obj, list *ppdl)
Definition: misc.c:781
const char * entity_local_name(entity e)
entity_local_name modified so that it does not core when used in vect_fprint, since someone thought t...
Definition: entity.c:453
#define normalized_undefined
Definition: ri.h:1745
#define expression_domain
newgen_execution_domain_defined
Definition: ri.h:154
#define REFERENCE(x)
REFERENCE.
Definition: ri.h:2296
#define normalized_complex_p(x)
Definition: ri.h:1782
#define reference_variable(x)
Definition: ri.h:2326
#define reference_undefined_p(x)
Definition: ri.h:2303
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define reference_domain
newgen_range_domain_defined
Definition: ri.h:338
#define expression_undefined
Definition: ri.h:1223
#define expression_normalized(x)
Definition: ri.h:1249
#define expression_undefined_p(x)
Definition: ri.h:1224
#define reference_domain_number(x)
Definition: ri.h:2324
#define normalized_linear(x)
Definition: ri.h:1781
#define expression_syntax(x)
Definition: ri.h:1247
#define expression_domain_number(x)
Definition: ri.h:1245
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
string words_to_string(cons *lw)
Definition: print.c:211
void print_words(FILE *fd, cons *lw)
Definition: print.c:263
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207