PIPS
allocatable.c
Go to the documentation of this file.
1 /*
2 
3  $Id: allocatable.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6  Copyright 2009-2010 HPC Project
7 
8  This file is part of PIPS.
9 
10  PIPS is free software: you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  any later version.
14 
15  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
16  WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  FITNESS FOR A PARTICULAR PURPOSE.
18 
19  See the GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
23 
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "pips_config.h"
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "genC.h"
34 #include "linear.h"
35 #include "misc.h"
36 #include "ri.h"
37 
38 #include "ri-util.h"
39 #include "prettyprint.h"
40 
41 /**
42  * @brief Helper for creating an allocatable structure. Here we create the
43  * field corresponding to the data array.
44  */
46  const char *struct_name,
47  const char *name,
48  list dimensions) {
49 
50  string field ;
51  asprintf(&field, "%s" MEMBER_SEP_STRING"%s", struct_name,name);
52 
53  pips_assert("Trying to create data for an already existing struct ?",
55 
57  free(field);
58  entity_type(data) = make_type_variable(make_variable(b, dimensions, NULL));
61 
62  return data;
63 }
64 
65 static entity make_bound(const char *struct_name, const char *lname, int suffix) {
66  entity bound;
67 
68  // Create the name
69  string name;
70  pips_assert("asprintf !",
71  asprintf( &name,
72  "%s" MEMBER_SEP_STRING "%s%d",
73  struct_name,
74  lname,
75  suffix ));
76 
77  pips_assert("Trying to create lower bound but already existing ?",
79 
80  bound = FindOrCreateTopLevelEntity(name);
81 
83  NULL,
84  NULL));
87 
88  free(name);
89  return bound;
90 }
91 
92 /**
93  * @brief This function try to find the allocatable structure corresponding to
94  * the number of dimensions requested, and create it if necessary.
95  * @param name is the name of the array (prettyprint name)
96  */
97 entity find_or_create_allocatable_struct(basic b, string name, int ndim) {
98  // FI: scaffolding code is not even guarded...
99  printf("Creating allocatable struct for dim %d\n", ndim);
100 
101  // Create the entity name according to the number of dims
102  string struct_name;
103  string b_str = STRING(CAR(words_basic(b,NULL)));
104  pips_assert("asprintf !",
105  asprintf( &struct_name, ALLOCATABLE_PREFIX"%s_%s_%dD", name, b_str,ndim));
106 
107  // Here is the internal PIPS name, there is a prefix for struct
108  string prefixed_name = strdup(concatenate(STRUCT_PREFIX, struct_name, NULL));
109 
110  // Let's try to localize the structure
111  entity struct_entity = FindEntity(TOP_LEVEL_MODULE_NAME, prefixed_name);
112 
113  // Localization failed, let's create it
114  if(struct_entity == entity_undefined) {
115  list fields = NULL;
116  list dimensions = NULL;
117  for (int dim = ndim; dim >= 1; dim--) {
118  entity lower = make_bound(struct_name, ALLOCATABLE_LBOUND_PREFIX, dim);
119  entity upper = make_bound(struct_name, ALLOCATABLE_UBOUND_PREFIX, dim);
120 
121  // Field for struct
122  fields = CONS(ENTITY,lower,fields);
123  fields = CONS(ENTITY,upper,fields);
124 
125  // Dimensions for the data array
127  entity_to_expression(upper),
128  NIL);
129  dimensions = CONS(DIMENSION,d,dimensions );
130  }
131 
132  // Create data holder
133  fields
134  = CONS(ENTITY,make_data_field(b, struct_name, name, dimensions),fields);
135 
136  // Create the struct
137  string field;
138  asprintf(&field,STRUCT_PREFIX "%s",struct_name);
139  struct_entity = FindOrCreateTopLevelEntity(field);
140  free(field);
141  entity_type(struct_entity) = make_type_struct(fields);
142  entity_storage(struct_entity) = make_storage_rom();
143  entity_initial(struct_entity) = make_value_unknown();
144  }
145 
146  free(prefixed_name);
147  free(struct_name);
148 
149  return struct_entity;
150 }
value make_value_unknown(void)
Definition: ri.c:2847
type make_type_variable(variable _field_)
Definition: ri.c:2715
storage make_storage_rom(void)
Definition: ri.c:2285
type make_type_struct(list _field_)
Definition: ri.c:2730
basic make_basic_int(intptr_t _field_)
Definition: ri.c:158
dimension make_dimension(expression a1, expression a2, list a3)
Definition: ri.c:565
variable make_variable(basic a1, list a2, list a3)
Definition: ri.c:2895
#define STRING(x)
Definition: genC.h:87
void free(void *)
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define asprintf
Definition: misc-local.h:225
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define TOP_LEVEL_MODULE_NAME
Module containing the global variables in Fortran and C.
Definition: naming-local.h:101
#define MEMBER_SEP_STRING
Definition: naming-local.h:53
#define STRUCT_PREFIX
Definition: naming-local.h:56
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
struct _newgen_struct_data_ * data
static entity make_data_field(basic b, const char *struct_name, const char *name, list dimensions)
Helper for creating an allocatable structure.
Definition: allocatable.c:45
entity find_or_create_allocatable_struct(basic b, string name, int ndim)
This function try to find the allocatable structure corresponding to the number of dimensions request...
Definition: allocatable.c:97
static entity make_bound(const char *struct_name, const char *lname, int suffix)
Definition: allocatable.c:65
list words_basic(basic obj, list *ppdl)
what about simple DOUBLE PRECISION, REAL, INTEGER...
Definition: declarations.c:323
#define ALLOCATABLE_UBOUND_PREFIX
#define ALLOCATABLE_PREFIX
#define ALLOCATABLE_LBOUND_PREFIX
entity FindEntity(const char *package, const char *name)
Retrieve an entity from its package/module name and its local name.
Definition: entity.c:1503
entity FindOrCreateTopLevelEntity(const char *name)
Return a top-level entity.
Definition: entity.c:1603
expression entity_to_expression(entity e)
if v is a constant, returns a constant call.
Definition: expression.c:165
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define entity_storage(x)
Definition: ri.h:2794
#define entity_undefined
Definition: ri.h:2761
#define entity_type(x)
Definition: ri.h:2792
#define entity_domain
newgen_syntax_domain_defined
Definition: ri.h:410
#define entity_initial(x)
Definition: ri.h:2796
char * strdup()
int printf()
static int lname(char *s, int look_for_entry)
check for keywords for subprograms return 0 if comment card, 1 if found name and put in arg string.
Definition: split_file.c:283
The structure used to build lists in NewGen.
Definition: newgen_list.h:41