summaryrefslogtreecommitdiff
path: root/cloog-0.17.0/source/statement.c
blob: 1c6af4ede13be46f16c64d24590cb85731189670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

   /**-------------------------------------------------------------------**
    **                              CLooG                                **
    **-------------------------------------------------------------------**
    **                           statement.c                             **
    **-------------------------------------------------------------------**
    **                 First version: november 4th 2001                  **
    **-------------------------------------------------------------------**/


/******************************************************************************
 *               CLooG : the Chunky Loop Generator (experimental)             *
 ******************************************************************************
 *                                                                            *
 * Copyright (C) 2001-2005 Cedric Bastoul                                     *
 *                                                                            *
 * This library is free software; you can redistribute it and/or              *
 * modify it under the terms of the GNU Lesser General Public                 *
 * License as published by the Free Software Foundation; either               *
 * version 2.1 of the License, or (at your option) any later version.         *
 *                                                                            *
 * This library 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          *
 * Lesser General Public License for more details.                            *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public           *
 * License along with this library; if not, write to the Free Software        *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,                         *
 * Boston, MA  02110-1301  USA                                                *
 *                                                                            *
 * CLooG, the Chunky Loop Generator                                           *
 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr                         *
 *                                                                            *
 ******************************************************************************/
/* CAUTION: the english used for comments is probably the worst you ever read,
 *          please feel free to correct and improve it !
 */

# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "../include/cloog/cloog.h"


/******************************************************************************
 *                             Memory leaks hunting                           *
 ******************************************************************************/


/**
 * These functions and global variables are devoted to memory leaks hunting: we
 * want to know at each moment how many CloogStatement structures had been
 * allocated (cloog_statement_allocated) and how many had been freed
 * (cloog_statement_freed). Each time a CloogStatement structure is allocated,
 * a call to the function cloog_statement_leak_up() must be carried out, and
 * respectively cloog_statement_leak_down() when a CloogStatement structure is
 * freed. The special variable cloog_statement_max gives the maximal number of
 * CloogStatement structures simultaneously alive (i.e. allocated and
 * non-freed) in memory.
 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
 */


static void cloog_statement_leak_up(CloogState *state)
{
  state->statement_allocated++;
  if ((state->statement_allocated - state->statement_freed) > state->statement_max)
  state->statement_max = state->statement_allocated - state->statement_freed ;
}


static void cloog_statement_leak_down(CloogState *state)
{ 
  state->statement_freed++;
}


/******************************************************************************
 *                          Structure display function                        *
 ******************************************************************************/


/**
 * cloog_domain_print_structure :
 * this function is a human-friendly way to display the CloogDomain data
 * structure, it includes an indentation level (level) in order to work with
 * others print_structure functions.
 * - June  16th 2005: first version.
 */
void cloog_statement_print_structure(file, statement, level)
FILE * file ;
CloogStatement * statement ;
int level ;
{ int i ;
      
  if (statement != NULL)
  { /* Go to the right level. */
    for (i=0; i<level; i++)
    fprintf(file,"|\t") ;
    fprintf(file,"+-- CloogStatement %d \n",statement->number) ;
    
    statement = statement->next ;
 
    while (statement != NULL)
    { for (i=0; i<level; i++)
      fprintf(file,"|\t") ;
      fprintf(file,"|          |\n");
      for (i=0; i<level; i++)
      fprintf(file,"|\t") ;
      fprintf(file,"|          V\n");
      
      for (i=0; i<level; i++)
      fprintf(file,"|\t") ;
      fprintf(file,"|   CloogStatement %d \n",statement->number) ;
      statement = statement->next ;
    }
  }
  else
  { for (i=0; i<level; i++)
    fprintf(file,"|\t") ;
    
    fprintf(file,"+-- No CloogStatement\n") ;
  }  
}


/**
 * cloog_statement_print function:
 * This function prints the content of a CloogStatement structure (statement)
 * into a file (file, possibly stdout).
 */
void cloog_statement_print(FILE * file, CloogStatement * statement)
{ cloog_statement_print_structure(file,statement,0) ;
}


/******************************************************************************
 *                         Memory deallocation function                       *
 ******************************************************************************/


/**
 * cloog_statement_free function:
 * This function frees the allocated memory for a CloogStatement structure.
 */
void cloog_statement_free(CloogStatement * statement)
{ CloogStatement * next ;

  while (statement != NULL) {
    cloog_statement_leak_down(statement->state);
    
    next = statement->next ;
    /* free(statement->usr) ; Actually, this is user's job ! */
    free(statement->name);
    free(statement) ;
    statement = next ;
  }
}


/******************************************************************************
 *                            Processing functions                            *
 ******************************************************************************/


/**
 * cloog_statement_malloc function:
 * This function allocates the memory space for a CloogStatement structure and
 * sets its fields with default values. Then it returns a pointer to the
 * allocated space.
 * - November 21th 2005: first version.
 */
CloogStatement *cloog_statement_malloc(CloogState *state)
{ CloogStatement * statement ;
  
  /* Memory allocation for the CloogStatement structure. */
  statement = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
  if (statement == NULL) 
    cloog_die("memory overflow.\n");
  cloog_statement_leak_up(state);
  
  /* We set the various fields with default values. */
  statement->state = state;
  statement->number = 0;
  statement->name = NULL;
  statement->usr  = NULL ; /* To fill it is actually user's job ! */
  statement->next = NULL ;
  
  return statement ;
}  


/**
 * cloog_statement_alloc function:
 * This function allocates the memory space for a CloogStatement structure and
 * sets its fields with those given as input. Then it returns a pointer to the
 * allocated space.
 * - number is the statement number.
 **
 * - September 9th 2002: first version.
 * - March    17th 2003: fix for the usr field in CloogStatement structure.
 * - April    16th 2005: adaptation to new CloogStatement structure (with
 *                       number), cloog_statement_read becomes
 *                       cloog_statement_alloc sincethere is nothing more to
 *                       read on a file.
 * - November 21th 2005: use of cloog_statement_malloc.
 */
CloogStatement *cloog_statement_alloc(CloogState *state, int number)
{ CloogStatement * statement ;
    
  /* Memory allocation and initialization of the structure. */
  statement = cloog_statement_malloc(state);

  statement->number = number ;
  
  return statement ;
}


/**
 * cloog_statement_copy function:
 * This function returns a copy of the CloogStatement structure given as input.
 * - October 28th 2001: first version (in loop.c). 
 * - March   17th 2003: fix for the usr field in CloogStatement structure.
 * - April   16th 2005: adaptation to new CloogStatement struct (with number). 
 */ 
CloogStatement * cloog_statement_copy(CloogStatement * source)
{ CloogStatement * statement, * temp, * now = NULL ;
  
  statement = NULL ;

  while (source != NULL) {
    cloog_statement_leak_up(source->state);

    temp = (CloogStatement *)malloc(sizeof(CloogStatement)) ;
    if (temp == NULL)
      cloog_die("memory overflow.\n");
    
    temp->state  = source->state;
    temp->number = source->number ;
    temp->name = source->name ? strdup(source->name) : NULL;
    temp->usr    = source->usr ;
    temp->next   = NULL ;
    
    if (statement == NULL)
    { statement = temp ;
      now = statement ;
    }
    else
    { now->next = temp ;
      now = now->next ;
    }
    source = source->next ;
  }
  return(statement) ;
}


/** 
 * cloog_statement_add function:
 * This function adds a CloogStatement structure (statement) at a given place
 * (now) of a NULL terminated list of CloogStatement structures. The beginning
 * of this list is (start). This function updates (now) to (loop), and
 * updates (start) if the added element is the first one -that is when (start)
 * is NULL-.
 * - March 27th 2004: first version. 
 */ 
void cloog_statement_add(start, now, statement)
CloogStatement ** start, ** now, * statement ;
{ if (*start == NULL)
  { *start = statement ;
    *now = *start ;
  }
  else
  { (*now)->next = statement ;
    *now = (*now)->next ;
  }
}