PIPS
Pvecteur.c
Go to the documentation of this file.
1 /*
2 
3  $Id: Pvecteur.c 23065 2016-03-02 09:05:50Z 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  * NewGen interface with C3 type Pvecteur for PIPS project
29  *
30  * Remi Triolet
31  *
32  * Bugs:
33  *
34  * - the NewGen interface makes it very cumbersome; function f()
35  * prevents a very simple fscanf() with format %d and %s and
36  * implies a copy in a temporary buffer (Francois Irigoin)
37  * - fixed for '\ )' in tokens (FC 2001/07/06)
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "linear.h"
45 #include "genC.h"
46 #include "ri.h"
47 #include "misc.h"
48 
49 #define TCST_OLD_NAME "TERME_CONSTANT" /* compatibility to read old bases */
50 #define TCST_NAME "."
51 
52 /* print token */
53 static void print_token(FILE * fd, string s)
54 {
55  for (; *s; s++)
56  {
57  /* backslashify '\ )' */
58  if (*s=='\\' || *s==' ' || *s==')')
59  {
60  putc('\\', fd);
61  }
62  putc(*s, fd);
63  }
64 }
65 
66 /* stop at ' ' or ')'.
67  * handles '\' as a protection.
68  * returns a pointer to a static buffer.
69  */
70 static string read_token(int (*f)())
71 {
72  /* static internal buffer */
73  static string buf = NULL;
74  static int bufsize = 0;
75  int index = 0, c;
76 
77  if (!buf)
78  {
79  bufsize = 64; /* should be ok for most codes. */
80  buf = (char*) malloc(bufsize * sizeof(char));
81  pips_assert("malloc ok", buf);
82  }
83 
84  while ((c = f()) != -1 && c!=' ' && c!=')')
85  {
86  if (index+1>=bufsize) /* for current char and ending 0 */
87  {
88  bufsize *= 2;
89  buf = (char*) realloc(buf, bufsize * sizeof(char));
90  pips_assert("realloc ok", buf);
91  }
92 
93  if (c == '\\')
94  {
95  c = f();
96  pips_assert("there is a char after a backslash", c!=-1);
97  }
98 
99  buf[index++] = (char) c;
100  }
101  buf[index++] = '\0';
102 
103  return buf;
104 }
105 
106 /* output is "([val var ]* )"
107  */
108 void vect_gen_write(FILE *fd, Pvecteur v)
109 {
110  Pvecteur p;
111 
112  putc('(', fd);
113  for (p = v; p != NULL; p = p->succ)
114  {
115  fprint_Value(fd, val_of(p));
116  putc(' ', fd);
117  print_token(fd, (p->var == (Variable) 0) ? TCST_NAME :
118  entity_name((entity) p->var));
119  putc(' ', fd);
120  }
121  putc(')', fd);
122 }
123 
124 Pvecteur vect_gen_read(FILE * fd __attribute__ ((unused)),
125  int (*f)())
126 {
127  Pvecteur p = NULL;
128  string svar, sval;
129  Variable var;
130  Value val;
131  int openpar;
132 
133  openpar = f();
134  pips_assert("vect starts with '('", openpar=='(');
135 
136  while ((sval = read_token(f)) && *sval)
137  {
138  sscan_Value(sval, &val);
139  svar = read_token(f);
140 
141  if (same_string_p(svar, TCST_NAME) ||
143  {
144  var = (Variable) 0;
145  }
146  else
147  {
149  pips_assert("valid variable entity", !entity_undefined_p((entity)var));
150  }
151  vect_add_elem(&p, var, val);
152  }
153 
154  p = vect_reversal(p);
155  return p;
156 }
157 
159 {
160  vect_rm(v);
161 }
162 
164 {
165  return vect_copy(v);
166 }
167 
169 {
170  int result = 0;
171  for (; v; v=v->succ)
172  result += sizeof(Svecteur);
173  return result;
174 }
175 
177 {
178  int result = 0;
179  for(; pc; pc=pc->succ)
180  result += sizeof(Scontrainte) +
182  return result;
183 }
184 
185 /* That is all
186  */
float a2sf[2] __attribute__((aligned(16)))
USER generates a user error (i.e., non fatal) by printing the given MSG according to the FMT.
Definition: 3dnow.h:3
Pvecteur vect_gen_read(FILE *fd __attribute__((unused)), int(*f)())
Definition: Pvecteur.c:124
int contrainte_gen_allocated_memory(Pcontrainte pc)
Definition: Pvecteur.c:176
static void print_token(FILE *fd, string s)
print token
Definition: Pvecteur.c:53
#define TCST_OLD_NAME
Definition: Pvecteur.c:49
static string read_token(int(*f)())
stop at ' ' or ')'.
Definition: Pvecteur.c:70
Pvecteur vect_gen_copy_tree(Pvecteur v)
Definition: Pvecteur.c:163
void vect_gen_write(FILE *fd, Pvecteur v)
output is "([val var ]* )"
Definition: Pvecteur.c:108
#define TCST_NAME
Definition: Pvecteur.c:50
int vect_gen_allocated_memory(Pvecteur v)
Definition: Pvecteur.c:168
void vect_gen_free(Pvecteur v)
Definition: Pvecteur.c:158
int Value
void fprint_Value(FILE *, Value)
Definition: io.c:42
int sscan_Value(char *, Value *)
Definition: io.c:68
void * malloc(YYSIZE_T)
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define same_string_p(s1, s2)
void * gen_find_tabulated(const char *, int)
Definition: tabulated.c:218
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
Pvecteur vect_reversal(Pvecteur vect_in)
Pvecteur vect_reversal(Pvecteur vect_in); produces the reversal vector of the vect_in.
Definition: private.c:237
#define entity_undefined_p(x)
Definition: ri.h:2762
#define entity_name(x)
Definition: ri.h:2790
#define entity_domain
newgen_syntax_domain_defined
Definition: ri.h:410
static char buf[BSZ]
Definition: split_file.c:157
Pvecteur vecteur
struct Scontrainte * succ
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
Variable var
Definition: vecteur-local.h:90
struct Svecteur * succ
Definition: vecteur-local.h:92
#define val_of(varval)
void * Variable
arithmetique is a requirement for vecteur, but I do not want to inforce it in all pips files....
Definition: vecteur-local.h:60
Pbase vect_copy(Pvecteur b)
direct duplication.
Definition: alloc.c:240
void vect_rm(Pvecteur v)
void vect_rm(Pvecteur v): desallocation des couples de v;
Definition: alloc.c:78
void vect_add_elem(Pvecteur *pvect, Variable var, Value val)
void vect_add_elem(Pvecteur * pvect, Variable var, Value val): addition d'un vecteur colineaire au ve...
Definition: unaires.c:72