~ubuntu-branches/ubuntu/vivid/atlas/vivid

« back to all changes in this revision

Viewing changes to include/atlas_genparse.h

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-06-11 15:58:16 UTC
  • mfrom: (1.1.3 upstream)
  • mto: (2.2.21 experimental)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20130611155816-b72z8f621tuhbzn0
Tags: upstream-3.10.1
Import upstream version 3.10.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef ATLAS_GENPARSE_H
 
2
   #define ATLAS_GENPARSE_H
 
3
 
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include <assert.h>
 
7
#include <string.h>
 
8
#include <ctype.h>
 
9
#define NASMD 9
 
10
enum ASMDIA
 
11
   {ASM_None=0, gas_x86_32, gas_x86_64, gas_sparc, gas_ppc, gas_parisc,
 
12
    gas_mips, gas_arm, gas_s390};
 
13
static char *ASMNAM[NASMD] =
 
14
   {"",     "GAS_x8632", "GAS_x8664", "GAS_SPARC", "GAS_PPC", "GAS_PARISC",
 
15
    "GAS_MIPS", "GAS_ARM", "GAS_S390"};
 
16
/*
 
17
 * Basic data structure for forming queues with some minimal info
 
18
 */
 
19
typedef struct SIDNode ATL_sidnode_t;
 
20
struct SIDNode  /* holds string, integer, and double */
 
21
{
 
22
   double d;
 
23
   char *str;
 
24
   int i;
 
25
   ATL_sidnode_t *next;
 
26
};
 
27
 
 
28
#define SET_FLAG(bits_, flg_, val_) \
 
29
{\
 
30
   if (val_) (bits_) |= (1<<(flg_)); \
 
31
   else (bits_) &= ~(1<<(flg_)); \
 
32
}
 
33
#define FLAG_IS_SET(field_, bit_) ( ((field_) & (1<<(bit_))) != 0 )
 
34
 
 
35
/* procedure 1 : allocates ATL_sidnode_t */
 
36
static ATL_sidnode_t *ATL_NewSIDNode(void)
 
37
{
 
38
   ATL_sidnode_t *sp;
 
39
   sp = calloc(1, sizeof(ATL_sidnode_t));
 
40
   assert(sp);
 
41
   return(sp);
 
42
}
 
43
 
 
44
/* procedure 2 : allocates ATL_sidnode_t */
 
45
static ATL_sidnode_t *ATL_FreeSIDNode(ATL_sidnode_t *die)
 
46
{
 
47
   ATL_sidnode_t *sp=NULL;
 
48
   if (die)
 
49
   {
 
50
      sp = die->next;
 
51
      if (die->str)
 
52
         free(die->str);
 
53
      free(die);
 
54
   }
 
55
   return(sp);
 
56
}
 
57
 
 
58
/* procedure 3 */
 
59
static int GetL1CacheElts(char pre)
 
60
{
 
61
   FILE *L1f;
 
62
   int L1Size, i;
 
63
 
 
64
   L1f = fopen("res/L1CacheSize", "r");
 
65
   if (!L1f)
 
66
   {
 
67
      assert(system("make res/L1CacheSize\n") == 0);
 
68
      L1f = fopen("res/L1CacheSize", "r");
 
69
   }
 
70
   assert(L1f);
 
71
   assert(fscanf(L1f, "%d", &L1Size) == 1);
 
72
   fclose(L1f);
 
73
   assert(L1Size > 0);
 
74
   if (pre == 'c' || pre == 'd')
 
75
      i = 1024/8;
 
76
   else if (pre == 's')
 
77
      i = 1024/4;
 
78
   else if (pre == 'z')
 
79
      i = 1024/16;
 
80
   else
 
81
      assert(0);
 
82
   return(i*L1Size);
 
83
}
 
84
 
 
85
/* procedure 4 */
 
86
static char *DupString(char *str)
 
87
{
 
88
   int i,n;
 
89
   char *s;
 
90
 
 
91
   if (!str)
 
92
      return(NULL);
 
93
   n = strlen(str)+1;
 
94
   s = malloc(sizeof(char)*n);
 
95
   assert(s);
 
96
   for (i=0; i < n; i++)
 
97
      s[i] = str[i];
 
98
   return(s);
 
99
}
 
100
 
 
101
/* procedure 5 */
 
102
static char *GetSingleQuoteString(char *str)
 
103
{
 
104
   char *sp;
 
105
   int i, n;
 
106
 
 
107
   assert(str[0] == '\'');
 
108
   for (i=1; str[i] && str[i] != '\''; i++);
 
109
   assert(str[i]);
 
110
   sp = malloc(i*sizeof(char));
 
111
   for (n=i,i=1; i < n; i++)
 
112
      sp[i-1] = str[i];
 
113
   sp[n-1] = '\0';
 
114
   return(sp);
 
115
}
 
116
 
 
117
/* procedure 6 */
 
118
static int asmNames2bitfield(char *str)
 
119
/*
 
120
 * Takes str containing an assembly name list.  The list is ended by the first
 
121
 * white space or end of string.  List items are seperated by ',', and there
 
122
 * can be no whitespace in list.
 
123
 * RETURNS: bitfield with bits set corresponding to assemblies, 0 on error.
 
124
 */
 
125
{
 
126
   char asmname[64];
 
127
   int i, KeepOn, bits=0;
 
128
 
 
129
   do
 
130
   {
 
131
      for (i=0; !isspace(str[i]) && str[i] != ',' && str[i] && i < 64; i++)
 
132
         asmname[i] = str[i];
 
133
      asmname[i] = '\0';
 
134
      KeepOn = str[i] == ',';
 
135
      str += i+1;
 
136
      if (i >= 64)
 
137
         return(0);  /* no asm name > 63 in length */
 
138
      for (i=0; i < NASMD; i++)
 
139
      {
 
140
         if (!strcmp(ASMNAM[i], asmname))
 
141
         {
 
142
            bits |= (1<<i);
 
143
            break;
 
144
         }
 
145
      }
 
146
   }
 
147
   while(KeepOn);
 
148
   return(bits);
 
149
}
 
150
 
 
151
/* procedure 7 */
 
152
static int GetDoubleArr(char *str, int N, double *d)
 
153
/*
 
154
 * Reads in a list with form "%le,%le...,%le"; N-length d recieves doubles.
 
155
 * RETURNS: the number of doubles found, or N, whichever is less
 
156
 */
 
157
{
 
158
   int i=1;
 
159
   assert(sscanf(str, "%le", d) == 1);
 
160
   while (i < N)
 
161
   {
 
162
      str = strstr(str, ",");
 
163
      if (!str)
 
164
         break;
 
165
      str++;
 
166
      assert(sscanf(str, "%le", d+i) == 1);
 
167
      i++;
 
168
   }
 
169
   return(i);
 
170
}
 
171
 
 
172
/* procedure 8 */
 
173
static char *GetLongerString(char *shrt, int newlen)
 
174
/*
 
175
 * Allocates new string of size newlen, copies shrt into it, and frees shrt.
 
176
 */
 
177
{
 
178
   char *sp;
 
179
 
 
180
   sp = malloc(sizeof(char)*newlen);
 
181
   assert(sp);
 
182
   if (shrt)
 
183
   {
 
184
      strcpy(sp, shrt);
 
185
      free(shrt);
 
186
   }
 
187
   else if (newlen >= 0)
 
188
      sp[0] = '\0';
 
189
   return(sp);
 
190
}
 
191
 
 
192
/* procedure 9 */
 
193
static char *GetOneLine(FILE *fpin)
 
194
/*
 
195
 * RETURNS: string of one line from stream fpin,  NULL if stream exhausted.
 
196
 */
 
197
{
 
198
   const int inc=256;
 
199
   static int len=0;
 
200
   static char *ln, *sp;
 
201
   int i, j, KeepOn;
 
202
 
 
203
   if (!len)
 
204
   {
 
205
      ln = malloc(inc*sizeof(char));
 
206
      assert(ln);
 
207
      len = inc;
 
208
   }
 
209
   if (!fgets(ln, len, fpin))
 
210
      return(NULL);
 
211
 
 
212
   for (i=0; ln[i]; i++);  /* find end of string */
 
213
   if (!i) return(ln);
 
214
   while (ln[i-1] != '\n')    /* if last char not \n, read rest of line */
 
215
   {
 
216
      len += inc;
 
217
      ln = GetLongerString(ln, len);
 
218
      if (!fgets(ln+i, inc, fpin))
 
219
         return(ln);
 
220
       for (; ln[i]; i++);  /* find end of string */
 
221
   }
 
222
   return(ln);
 
223
}
 
224
 
 
225
/* procedure 10 */
 
226
static char *GetJoinedLines(FILE *fpin)
 
227
/*
 
228
 * Gets lines from file fpin; if last non-whitespace char is '\', joins lines
 
229
 * RETURNS: line from file including joining, NULL if fpin exhausted
 
230
 */
 
231
{
 
232
   char *ln, *sp;
 
233
   static char *join=NULL;
 
234
   static int jlen=0;
 
235
   int i, j, k;
 
236
 
 
237
   sp = ln = GetOneLine(fpin);
 
238
   if (!sp)
 
239
      return(NULL);
 
240
   j = 0;   /* current length of join string */
 
241
   if (ln)
 
242
   {
 
243
      for (i=0; ln[i]; i++);  /* find end of string */
 
244
      if (!i) return(NULL);
 
245
      for (i--; isspace(ln[i]) && i > 0; i--);  /* find last non-wspace char */
 
246
      while (ln[i] == '\\')
 
247
      {
 
248
         if (jlen < j+i+3)
 
249
         {
 
250
            jlen = j+i+i+3;
 
251
            join = GetLongerString(join, jlen);
 
252
         }
 
253
         for (k=0; k < i; k++)
 
254
            join[j+k] = ln[k];
 
255
         j += k;
 
256
         join[j++] = ' ';
 
257
         join[j] = '\0';
 
258
         ln = GetOneLine(fpin);   /* get new line that should be joined */
 
259
         assert(ln);              /* can't end file with continue */
 
260
         for (i=0; ln[i]; i++);   /* find end of new line */
 
261
         for (i--; isspace(ln[i]) && i > 0; i--); /* find last non-wspc char */
 
262
         sp = join;
 
263
      }
 
264
      if (sp == join)
 
265
      {
 
266
         if (jlen < j+i+3)
 
267
         {
 
268
            jlen = j+i+i+3;
 
269
            join = GetLongerString(join, jlen);
 
270
         }
 
271
         for (k=0; k <= i; k++)
 
272
            join[j+k] = ln[k];
 
273
         j += k;
 
274
         join[j] = '\n';
 
275
         join[j+1] = '\0';
 
276
         sp = join;
 
277
      }
 
278
   }
 
279
   return(sp);
 
280
}
 
281
 
 
282
/* procedure 11 */
 
283
static char *GetGoodGcc()
 
284
/*
 
285
 * Gets gcc path and name along with mandatory flags (-g/-m64/-pg,etc) by
 
286
 * querying Make.inc setting
 
287
 */
 
288
{
 
289
   static char gcc[2048];
 
290
   static int INIT=0;
 
291
   if (!INIT)
 
292
   {
 
293
      FILE *fpin;
 
294
      assert(system("make res/goodgcc.txt > /dev/null 2>&1") == 0);
 
295
      fpin = fopen("res/goodgcc.txt", "r");
 
296
      assert(fpin);
 
297
      assert(fscanf(fpin, "'%[^\']", gcc) == 1);
 
298
      fclose(fpin);
 
299
   }
 
300
   return(gcc);
 
301
}
 
302
 
 
303
static char *GetKCFlags(char pre)
 
304
/*
 
305
 * Gets flags being used for <pre>KCFLAGS
 
306
 */
 
307
{
 
308
   char ln[4096];
 
309
   FILE *fpin;
 
310
   int i;
 
311
 
 
312
   if (pre == 'z')
 
313
      pre = 'd';
 
314
   else if (pre == 'c')
 
315
      pre = 's';
 
316
   i = system("rm -f res/kcflags.txt");
 
317
   sprintf(ln, "grep \"%cKCFLAGS = \" Make.inc | sed s/%cKCFLAGS\\ =\\ // > res/kcflags.txt", toupper(pre), toupper(pre));
 
318
   assert(system(ln) == 0);
 
319
   fpin = fopen("res/kcflags.txt", "r");
 
320
   assert(fpin);
 
321
   assert(fgets(ln, 4096, fpin) != NULL);
 
322
   fclose(fpin);
 
323
/*
 
324
 * Get rid of trailing and leading whitespaces
 
325
 */
 
326
   for (i=0; ln[i]; i++);
 
327
   for (i--; isspace(ln[i]); i--);
 
328
   ln[i+1] = '\0';
 
329
   for (i=0; isspace(ln[i]); i++);
 
330
   return(DupString(ln+i));
 
331
}
 
332
#endif /* end atlas_genparse.h guard */