PIPS
util.c
Go to the documentation of this file.
1 /*
2 
3  $Id: util.c 23412 2017-08-09 15:07:09Z irigoin $
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 /* Pot-pourri of utilities for the internal representation.
28  * Some functions could be moved to non-generic files such as entity.c.
29  */
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include "genC.h"
34 #include "linear.h"
35 
36 #include "misc.h"
37 #include "ri.h"
38 
39 #include "ri-util.h"
40 
41 /* To deal with labels */
42 
44 {
47 
48  pips_debug(5, "searched entity: %s\n", full);
49  void * found = gen_find_tabulated(full, entity_domain);
50  return (entity) (gen_chunk_undefined_p(found) ? entity_undefined : found);
51 }
52 
53 /* To find resources (this should be located in workspace-util as it
54  * depends both on pipsdbm and ri-util.
55  */
56 
58 {
60 }
61 
63 {
65 }
66 
68 {
70 }
71 
73 {
75 }
76 
78 {
80 }
81 
83 {
85 }
86 
88 {
90 }
91 
92 ␌
94 {
96 
97  if (! entity_module_p(e)) {
98  pips_internal_error("entity %s is not a module",
99  entity_name(e));
100  }
101  while (pv != NIL) {
102  entity v = ENTITY(CAR(pv));
103  type tv = entity_type(v);
104  storage sv = entity_storage(v);
105  // FI: locations.c should be part of ri-util or a large entity library
106  value val = entity_initial(v); // To check location entities
107 
108  // FI: the initial value of formal parameters may be value_undefined...
109  // See Semantics-New/block01.c, formal parameter i of multiply
110  if (type_variable_p(tv)
111  && storage_formal_p(sv)
112  && (value_undefined_p(val) || !value_reference_p(val))) {
113  if (formal_offset(storage_formal(sv)) == i) {
114  return(v);
115  }
116  }
117 
118  pv = CDR(pv);
119  }
120 
121  return(entity_undefined);
122 }
123 
124 /* returns true if v is the ith formal parameter of function f */
126 {
127  type tv = entity_type(v);
128  storage sv = entity_storage(v);
129 
130  if (! entity_module_p(f)) {
131  pips_internal_error("[ith_parameter_p] %s is not a module\n", entity_name(f));
132  }
133 
134  if (type_variable_p(tv) && storage_formal_p(sv)) {
135  formal fv = storage_formal(sv);
136  return(formal_function(fv) == f && formal_offset(fv) == i);
137  }
138 
139  return(false);
140 }
141 ␌
142 /* functions for references */
143 
144 /* returns the ith index of an array reference */
146 {
147  int count = i;
148  cons *pi = reference_indices(ref);
149 
150  while (pi != NIL && --count > 0)
151  pi = CDR(pi);
152 
153  pips_assert("reference_ith_index", pi != NIL);
154 
155  return(EXPRESSION(CAR(pi)));
156 }
157 
158 /* Test if a string can be a Fortran 77 comment: */
159 bool comment_string_p(const string comment)
160 {
161  char c = *comment;
162  /* If a line begins with a non-space character, claims it may be a
163  Fortran comment. Assume empty line are comments. */
164  return c != '\0' && c != ' ' && c != '\t';
165 }
166 
167 
168 /* Remove trailing line feed if any */
170 {
171  int sl = strlen(s);
172  if(sl>0) {
173  string ntl = s+sl-1;
174  if(sl>0 && *ntl=='\n') {
175  *ntl='\000';
176  }
177  }
178  return s;
179 }
180 
181 /* Remove trailing line feeds
182  *
183  * This function has been implemented three times. See below
184  * string_strip_final_linefeeds() and string_fuse_final_linefeeds().
185  */
187 {
188  int sl = strlen(s);
189  if(sl>0) {
190  string ntl = s+sl-1;
191  while(sl>0 && *ntl=='\n') {
192  *ntl='\000';
193  ntl--;
194  sl--;
195  }
196  }
197  return s;
198 }
199 
200 
201 /* Get rid of linefeed/newline at the end of a string.
202  *
203  * This is sometimes useful to cleanup comments messed up by the
204  * lexical analyzer.
205  *
206  * Warning: the argument s is updated if it ends up with LF
207  */
209 {
210  int l = strlen(s)-1;
211 
212  while(l>=0 && *(s+l)=='\n') {
213  *(s+l) = '\000';
214  l--;
215  }
216 
217  return s;
218 }
219 
220 /* Get rid of extra linefeed/newline at the end of a string.
221  *
222  * This is sometimes useful to cleanup comments messed up by the
223  * lexical analyzer.
224  *
225  * Warning: the argument s is updated if it ends up with LF
226  */
228 {
229  int l = strlen(s)-1;
230 
231  while(l>=1 && *(s+l)=='\n' && *(s+l-1)=='\n') {
232  *(s+l) = '\000';
233  l--;
234  }
235 
236  return s;
237 }
238 
static int count
Definition: SDG.c:519
static reference ref
Current stmt (an integer)
Definition: adg_read_paf.c:163
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
string string_entitiesfilename(const char *s)
Definition: file_names.c:87
string string_predicat_fortranfilename(const char *s)
Definition: file_names.c:81
string string_fortranfilename(const char *s)
Definition: file_names.c:49
string string_pp_fortranfilename(const char *s)
Definition: file_names.c:75
string string_par_codefilename(const char *s)
Definition: file_names.c:43
string string_par_fortranfilename(const char *s)
Definition: file_names.c:69
string string_codefilename(const char *s)
Naming of files in the PIPS database.
Definition: file_names.c:37
static void comment(string_buffer code, spoc_hardware_type hw, dagvtx v, int stage, int side, bool flip)
Definition: freia_spoc.c:52
#define gen_chunk_undefined_p(c)
Definition: genC.h:75
#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
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
#define LABEL_PREFIX
Definition: naming-local.h:31
#define MODULE_SEP_STRING
Definition: naming-local.h:30
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
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
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
bool entity_module_p(entity e)
Definition: entity.c:683
const char * label_local_name(entity e)
END_EOLE.
Definition: entity.c:604
string module_par_codefilename(entity e)
Definition: util.c:62
string module_par_fortranfilename(entity e)
Definition: util.c:72
bool ith_parameter_p(entity f, entity v, int i)
returns true if v is the ith formal parameter of function f
Definition: util.c:125
string string_remove_trailing_line_feed(string s)
Remove trailing line feed if any.
Definition: util.c:169
string string_fuse_final_linefeeds(string s)
Get rid of extra linefeed/newline at the end of a string.
Definition: util.c:227
string string_strip_final_linefeeds(string s)
Get rid of linefeed/newline at the end of a string.
Definition: util.c:208
string module_fortranfilename(entity e)
Definition: util.c:67
string string_remove_trailing_line_feeds(string s)
Remove trailing line feeds.
Definition: util.c:186
bool comment_string_p(const string comment)
Test if a string can be a Fortran 77 comment:
Definition: util.c:159
string module_predicat_fortranfilename(entity e)
Definition: util.c:82
expression reference_ith_index(reference ref, int i)
functions for references
Definition: util.c:145
string module_codefilename(entity e)
To find resources (this should be located in workspace-util as it depends both on pipsdbm and ri-util...
Definition: util.c:57
string module_entitiesfilename(entity e)
Definition: util.c:87
entity find_ith_parameter(entity e, int i)
Definition: util.c:93
entity find_label_entity(const char *module_name, const char *label_local_name)
Pot-pourri of utilities for the internal representation.
Definition: util.c:43
string module_pp_fortranfilename(entity e)
Definition: util.c:77
#define formal_offset(x)
Definition: ri.h:1408
#define value_undefined_p(x)
Definition: ri.h:3017
#define storage_formal_p(x)
Definition: ri.h:2522
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define entity_storage(x)
Definition: ri.h:2794
#define code_declarations(x)
Definition: ri.h:784
#define storage_formal(x)
Definition: ri.h:2524
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define entity_undefined
Definition: ri.h:2761
#define entity_name(x)
Definition: ri.h:2790
#define formal_function(x)
Definition: ri.h:1406
#define reference_indices(x)
Definition: ri.h:2328
#define value_code(x)
Definition: ri.h:3067
#define value_reference_p(x)
Definition: ri.h:3083
#define entity_type(x)
Definition: ri.h:2792
#define type_variable_p(x)
Definition: ri.h:2947
#define entity_domain
newgen_syntax_domain_defined
Definition: ri.h:410
#define entity_initial(x)
Definition: ri.h:2796
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
@ full
Definition: union-local.h:65