~ubuntu-branches/ubuntu/utopic/cloog/utopic

« back to all changes in this revision

Viewing changes to osl/source/extensions/extbody.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-02-26 14:21:11 UTC
  • mfrom: (3.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20140226142111-vsbb1isby30uundd
Tags: 0.18.2-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
    /*+-----------------------------------------------------------------**
3
 
     **                       OpenScop Library                          **
4
 
     **-----------------------------------------------------------------**
5
 
     **                      extensions/extbody.c                       **
6
 
     **-----------------------------------------------------------------**
7
 
     **                   First version: 07/12/2010                     **
8
 
     **-----------------------------------------------------------------**
9
 
 
10
 
 
11
 
 *****************************************************************************
12
 
 * OpenScop: Structures and formats for polyhedral tools to talk together    *
13
 
 *****************************************************************************
14
 
 *    ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__,                *
15
 
 *    /   / /  //  //  //  // /   / /  //  //   / /  // /  /|,_,             *
16
 
 *   /   / /  //  //  //  // /   / /  //  //   / /  // /  / / /\             *
17
 
 *  |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/  \            *
18
 
 *  | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\  \ /\           *
19
 
 *  | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\          *
20
 
 *  | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \  \         *
21
 
 *  | P |n| l | = | s | t |=| = |d| = | = | = | |   |=| o | | \# \  \        *
22
 
 *  | H | | y |   | e | o | | = |l|   |   | = | |   | | G | |  \  \  \       *
23
 
 *  | I | |   |   | e |   | |   | |   |   |   | |   | |   | |   \  \  \      *
24
 
 *  | T | |   |   |   |   | |   | |   |   |   | |   | |   | |    \  \  \     *
25
 
 *  | E | |   |   |   |   | |   | |   |   |   | |   | |   | |     \  \  \    *
26
 
 *  | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | /      \* \  \   *
27
 
 *  | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/        \  \ /   *
28
 
 *  '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---'          '--'    *
29
 
 *                                                                           *
30
 
 * Copyright (C) 2008 University Paris-Sud 11 and INRIA                      *
31
 
 *                                                                           *
32
 
 * (3-clause BSD license)                                                    *
33
 
 * Redistribution and use in source  and binary forms, with or without       *
34
 
 * modification, are permitted provided that the following conditions        *
35
 
 * are met:                                                                  *
36
 
 *                                                                           *
37
 
 * 1. Redistributions of source code must retain the above copyright notice, *
38
 
 *    this list of conditions and the following disclaimer.                  *
39
 
 * 2. Redistributions in binary form must reproduce the above copyright      *
40
 
 *    notice, this list of conditions and the following disclaimer in the    *
41
 
 *    documentation and/or other materials provided with the distribution.   *
42
 
 * 3. The name of the author may not be used to endorse or promote products  *
43
 
 *    derived from this software without specific prior written permission.  *
44
 
 *                                                                           *
45
 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
46
 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
48
 
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
49
 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
50
 
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51
 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
52
 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
53
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
54
 
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
55
 
 *                                                                           *
56
 
 * OpenScop Library, a library to manipulate OpenScop formats and data       *
57
 
 * structures. Written by:                                                   *
58
 
 * Cedric Bastoul     <Cedric.Bastoul@u-psud.fr> and                         *
59
 
 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr>                          *
60
 
 *                                                                           *
61
 
 *****************************************************************************/
62
 
 
63
 
#include <stdlib.h>
64
 
#include <stdio.h>
65
 
#include <string.h>
66
 
#include <ctype.h>
67
 
 
68
 
#include <osl/macros.h>
69
 
#include <osl/util.h>
70
 
#include <osl/body.h>
71
 
#include <osl/extensions/extbody.h>
72
 
 
73
 
 
74
 
/*+***************************************************************************
75
 
 *                          Structure display function                       *
76
 
 *****************************************************************************/
77
 
 
78
 
 
79
 
/**
80
 
 * osl_extbody_idump function:
81
 
 * this function displays an osl_extbody_t structure (*extbody) into a
82
 
 * file (file, possibly stdout) in a way that trends to be understandable. It
83
 
 * includes an indentation level (level) in order to work with others idump
84
 
 * functions.
85
 
 * \param[in] file   The file where the information has to be printed.
86
 
 * \param[in] ebody  The ebody structure to print.
87
 
 * \param[in] level  Number of spaces before printing, for each line.
88
 
 */
89
 
void osl_extbody_idump(FILE * file, osl_extbody_p ebody, int level) {
90
 
  int i, j;
91
 
 
92
 
  // Go to the right level.
93
 
  for (j = 0; j < level; j++)
94
 
    fprintf(file, "|\t");
95
 
 
96
 
  if (ebody != NULL)
97
 
    fprintf(file, "+-- osl_extbody_t\n");
98
 
  else
99
 
    fprintf(file, "+-- NULL extbody\n");
100
 
 
101
 
  if (ebody != NULL) {
102
 
    // Go to the right level.
103
 
    for(j = 0; j <= level; j++)
104
 
      fprintf(file, "|\t");
105
 
 
106
 
    // Display the number of ebody.
107
 
    fprintf(file, "nb_access: %d\n", ebody->nb_access);
108
 
 
109
 
    // Display the coordinates.
110
 
    for(i = 0; i < ebody->nb_access; i++) {
111
 
      // Go to the right level.
112
 
      for(j = 0; j <= level; j++)
113
 
        fprintf(file, "|\t");
114
 
 
115
 
      fprintf(file, "start: %d, length: %d\n",
116
 
              ebody->start[i], ebody->length[i]);
117
 
    }
118
 
 
119
 
    // Display the body
120
 
    osl_body_idump(file, ebody->body, level);
121
 
  }
122
 
 
123
 
  // The last line.
124
 
  for (j = 0; j <= level; j++)
125
 
    fprintf(file, "|\t");
126
 
  fprintf(file, "\n");
127
 
}
128
 
 
129
 
 
130
 
/**
131
 
 * osl_extbody_dump function:
132
 
 * this function prints the content of an osl_extbody_t structure
133
 
 * (*ebody) into a file (file, possibly stdout).
134
 
 * \param[in] file   The file where the information has to be printed.
135
 
 * \param[in] ebody The ebody structure to print.
136
 
 */
137
 
void osl_extbody_dump(FILE * file, osl_extbody_p ebody) {
138
 
  osl_extbody_idump(file, ebody, 0);
139
 
}
140
 
 
141
 
 
142
 
/**
143
 
 * osl_extbody_sprint function:
144
 
 * this function prints the content of an osl_extbody_t structure
145
 
 * (*ebody) into a string (returned) in the OpenScop textual format.
146
 
 * \param[in] ebody The ebody structure to print.
147
 
 * \return A string containing the OpenScop dump of the ebodystructure.
148
 
 */
149
 
char * osl_extbody_sprint(osl_extbody_p ebody) {
150
 
  int i;
151
 
  int high_water_mark = OSL_MAX_STRING;
152
 
  char * string = NULL, * body_string = NULL;
153
 
  char buffer[OSL_MAX_STRING];
154
 
 
155
 
  if (ebody != NULL) {
156
 
    OSL_malloc(string, char *, high_water_mark * sizeof(char));
157
 
    string[0] = '\0';
158
 
 
159
 
    sprintf(buffer, "# Number of accesses\n");
160
 
    osl_util_safe_strcat(&string, buffer, &high_water_mark);
161
 
 
162
 
    sprintf(buffer, "%d\n", ebody->nb_access);
163
 
    osl_util_safe_strcat(&string, buffer, &high_water_mark);
164
 
 
165
 
    if (ebody->nb_access) {
166
 
      sprintf(buffer, "# Access coordinates (start/length)\n");
167
 
      osl_util_safe_strcat(&string, buffer, &high_water_mark);
168
 
    }
169
 
    for (i = 0; i < ebody->nb_access; i++) {
170
 
      sprintf(buffer, "%d %d\n", ebody->start[i], ebody->length[i]);
171
 
      osl_util_safe_strcat(&string, buffer, &high_water_mark);
172
 
    }
173
 
 
174
 
    body_string = osl_body_sprint(ebody->body);
175
 
    osl_util_safe_strcat(&string, body_string, &high_water_mark);
176
 
    free(body_string);
177
 
  }
178
 
 
179
 
  return string;
180
 
}
181
 
 
182
 
 
183
 
/*****************************************************************************
184
 
 *                               Reading function                            *
185
 
 *****************************************************************************/
186
 
 
187
 
 
188
 
/**
189
 
 * osl_extbody_sread function:
190
 
 * this function reads an extbody structure from a string complying
191
 
 * to the OpenScop textual format and returns a pointer to this
192
 
 * extbody structure. The string should contain only one textual
193
 
 * format of an extbody structure. The input parameter is updated to
194
 
 * the position in the input string this function reach right after reading the
195
 
 * comment structure.
196
 
 * \param[in,out] input The input string where to find an extbody
197
 
 *                      structure.
198
 
 *                      Updated to the position after what has been read.
199
 
 * \return A pointer to the extbody structure that has been read.
200
 
 */
201
 
osl_extbody_p osl_extbody_sread(char ** input) {
202
 
  int k;
203
 
  int nb_access;
204
 
  osl_extbody_p ebody;
205
 
 
206
 
  if (input == NULL) {
207
 
    OSL_debug("no extbody optional tag");
208
 
    return NULL;
209
 
  }
210
 
 
211
 
  // Find the number of ebody provided.
212
 
  nb_access = osl_util_read_int(NULL, input);
213
 
 
214
 
  // Allocate the array of start and length.
215
 
  ebody = osl_extbody_malloc();
216
 
  OSL_malloc(ebody->start, int *, nb_access * sizeof(int));
217
 
  OSL_malloc(ebody->length, int *, nb_access * sizeof(int));
218
 
  ebody->nb_access = nb_access;
219
 
 
220
 
  // Get each array start/length.
221
 
  for (k = 0; k < nb_access; k++) {
222
 
    ebody->start[k] = osl_util_read_int(NULL, input);
223
 
    ebody->length[k] = osl_util_read_int(NULL, input);
224
 
  }
225
 
 
226
 
  // Read simple body.
227
 
  ebody->body = osl_body_sread(input);
228
 
 
229
 
  return ebody;
230
 
}
231
 
 
232
 
 
233
 
/*+***************************************************************************
234
 
 *                    Memory allocation/deallocation function                *
235
 
 *****************************************************************************/
236
 
 
237
 
 
238
 
/**
239
 
 * osl_extbody_malloc function:
240
 
 * this function allocates the memory space for an osl_extbody_t
241
 
 * structure and sets its fields with default values. Then it returns a
242
 
 * pointer to the allocated space.
243
 
 * \return A pointer to an empty extbody structure with fields set to
244
 
 *         default values.
245
 
 */
246
 
osl_extbody_p osl_extbody_malloc() {
247
 
  osl_extbody_p ebody;
248
 
  OSL_malloc(ebody, osl_extbody_p, sizeof(osl_extbody_t));
249
 
 
250
 
  ebody->nb_access = 0;
251
 
  ebody->start     = NULL;
252
 
  ebody->length    = NULL;
253
 
  ebody->body      = NULL;
254
 
 
255
 
  return ebody;
256
 
}
257
 
 
258
 
 
259
 
/**
260
 
 * osl_extbody_free function:
261
 
 * this function frees the allocated memory for an ebody structure.
262
 
 * \param[in,out] ebody The pointer to the extbody structure we want to free.
263
 
 */
264
 
void osl_extbody_free(osl_extbody_p ebody) {
265
 
  if (ebody != NULL) {
266
 
    free(ebody->start);
267
 
    free(ebody->length);
268
 
    osl_body_free(ebody->body);
269
 
    free(ebody);
270
 
  }
271
 
}
272
 
 
273
 
 
274
 
/*+***************************************************************************
275
 
 *                            Processing functions                           *
276
 
 *****************************************************************************/
277
 
 
278
 
 
279
 
/**
280
 
 * osl_extbody_clone function:
281
 
 * this function builds and returns a "hard copy" (not a pointer copy) of an
282
 
 * osl_extbody_t data structure.
283
 
 * \param[in] ebody The pointer to the extbody structure to clone.
284
 
 * \return A pointer to the clone of the extbody structure.
285
 
 */
286
 
osl_extbody_p osl_extbody_clone(osl_extbody_p ebody) {
287
 
  int i;
288
 
  osl_extbody_p clone;
289
 
 
290
 
  if (ebody == NULL)
291
 
    return NULL;
292
 
 
293
 
  clone = osl_extbody_malloc();
294
 
  clone->nb_access = ebody->nb_access;
295
 
  OSL_malloc(clone->start, int *, ebody->nb_access * sizeof(int));
296
 
  OSL_malloc(clone->length, int *, ebody->nb_access * sizeof(int));
297
 
 
298
 
  for (i = 0; i < ebody->nb_access; i++) {
299
 
    clone->start[i]  = ebody->start[i];
300
 
    clone->length[i] = ebody->length[i];
301
 
  }
302
 
 
303
 
  clone->body = osl_body_clone(ebody->body);
304
 
 
305
 
  return clone;
306
 
}
307
 
 
308
 
 
309
 
/**
310
 
 * osl_extbody_equal function:
311
 
 * this function returns true if the two extbody structures are the
312
 
 * same (content-wise), false otherwise. This functions considers two
313
 
 * extbody structures as equal if the order of the array start/length
314
 
 * differ, however the identifiers, start and length must be the same.
315
 
 * \param[in] e1 The first extbody structure.
316
 
 * \param[in] e2 The second extbody structure.
317
 
 * \return 1 if e1 and e2 are the same (content-wise), 0 otherwise.
318
 
 */
319
 
int osl_extbody_equal(osl_extbody_p e1, osl_extbody_p e2) {
320
 
  int i, j, found;
321
 
 
322
 
  if (e1 == e2)
323
 
    return 1;
324
 
 
325
 
  if (((e1 == NULL) && (e2 != NULL)) || ((e1 != NULL) && (e2 == NULL))) {
326
 
    OSL_info("extbody are not the same");
327
 
    return 0;
328
 
  }
329
 
 
330
 
  // Check whether the number of ebody is the same.
331
 
  if (e1->nb_access != e2->nb_access) {
332
 
    OSL_info("extbody are not the same");
333
 
    return 0;
334
 
  }
335
 
 
336
 
  // We accept a different order of the start/length, as long as the
337
 
  // identifiers are the same.
338
 
  for (i = 0; i < e1->nb_access; i++) {
339
 
    found = 0;
340
 
    for (j = 0; j < e2->nb_access; j++) {
341
 
      if ((e1->start[i] == e2->start[j]) &&
342
 
          (e1->length[i] == e2->length[j])) {
343
 
        found = 1;
344
 
        break;
345
 
      }
346
 
    }
347
 
    if (found != 1) {
348
 
      OSL_info("extbody are not the same");
349
 
      return 0;
350
 
    }
351
 
  }
352
 
 
353
 
  return osl_body_equal(e1->body, e2->body);
354
 
}
355
 
 
356
 
 
357
 
/**
358
 
 * osl_extbody_interface function:
359
 
 * this function creates an interface structure corresponding to the
360
 
 * extbody extension and returns it.
361
 
 * \return An interface structure for the extbody extension.
362
 
 */
363
 
osl_interface_p osl_extbody_interface() {
364
 
  osl_interface_p interface = osl_interface_malloc();
365
 
 
366
 
  interface->URI    = strdup(OSL_URI_EXTBODY);
367
 
  interface->idump  = (osl_idump_f)osl_extbody_idump;
368
 
  interface->sprint = (osl_sprint_f)osl_extbody_sprint;
369
 
  interface->sread  = (osl_sread_f)osl_extbody_sread;
370
 
  interface->malloc = (osl_malloc_f)osl_extbody_malloc;
371
 
  interface->free   = (osl_free_f)osl_extbody_free;
372
 
  interface->clone  = (osl_clone_f)osl_extbody_clone;
373
 
  interface->equal  = (osl_equal_f)osl_extbody_equal;
374
 
 
375
 
  return interface;
376
 
}
377
 
 
378
 
 
379
 
/**
380
 
 * osl_extbody_add function:
381
 
 * This function add an entry in the list of coordinates
382
 
 */
383
 
void osl_extbody_add(osl_extbody_p ebody, int start, int length) {
384
 
  ebody->nb_access++;
385
 
 
386
 
  OSL_realloc(ebody->start, int*, sizeof(int) * ebody->nb_access);
387
 
  OSL_realloc(ebody->length, int*, sizeof(int) * ebody->nb_access);
388
 
 
389
 
  ebody->start[ebody->nb_access-1] = start;
390
 
  ebody->length[ebody->nb_access-1] = length;
391
 
}