PIPS
step_bison_parser.y
Go to the documentation of this file.
1 %{
2 #ifdef HAVE_CONFIG_H
3  #include "pips_config.h"
4 extern char *step_lexertext;
5 extern int step_lexerlex();
6 extern int step_lexer_scan_string(char*);
7 extern int step_parse();
8 #else
9  #include "step_lexer.h"
10 #endif
11 
12 #include "defines-local.h"
13 
14 
15 extern void set_current_transform(int transform);
16 
17 extern entity entity_from_user_name(string name);
18 extern void remove_old_pragma(void);
19 extern step_directive begin_omp_construct(int type, string s);
20 extern step_directive end_omp_construct(int type);
21 
22 static step_directive concat_drt_clauses(step_directive drt, cons *liste_clauses);
23 static step_clause step_handle_reduction_clause(cons *ident_liste, int op);
24 static cons * step_add_to_ident_list(string name, cons * list);
25 
26 static string pragma_str_original=NULL;
27 
28 void step_lexererror(const char *s)
29 {
30  pips_user_error("\nParsing :\n%s\n%s at '%s'\n", pragma_str_original, s, step_lexertext);
31 }
32 
33 %}
34 
35 /* Bison declarations */
36 %union {
37  char* string;
38  cons * liste;
39  int integer;
40  step_directive step_directive;
41  step_clause step_clause;
42 }
43 %debug
44 
45 %token TK_EOL TK_ERROR
46 %token TK_LPAREN TK_RPAREN
47 %token TK_COLON TK_COMMA
48 
49 %token <string> TK_IDENT TK_COMMENT
50 %type <liste> ident_list string_list
51 
52 %token <integer> TK_OPERATOR
53 
54 %token <string> TK_RAW
55 
56 
57 %token TK_OMP_PRAGMA
58 %token <integer> TK_OMP_PARALLEL TK_OMP_LOOP TK_OMP_END TK_OMP_BARRIER TK_OMP_MASTER TK_OMP_SINGLE TK_OMP_THREADPRIVATE
59 %token TK_OMP_SHARED TK_OMP_PRIVATE TK_OMP_NOWAIT TK_OMP_REDUCTION TK_OMP_DEFAULT TK_OMP_COPYIN TK_OMP_FIRSTPRIVATE TK_OMP_SCHEDULE
60 
61 %type <step_directive> omp_directive
62 %type <liste> omp_parallel_clauses omp_loop_clauses omp_end_loop_clauses omp_parallel_loop_clauses
63 %type <step_clause> omp_shared omp_private omp_reduction omp_copyin omp_firstprivate omp_schedule
64 %type <liste> omp_threadprivate_listvar
65 
66 %token TK_STEP_PRAGMA
67 %token <integer> TK_STEP_TRANSFORMATION
68 
69 %%
70 
71 pragma:
72  TK_RAW { pips_user_warning("unknown pragma : %s\n", $1);}
73 
74 | TK_OMP_PRAGMA
75  { pips_debug(1,"OMP pragma begin\n"); remove_old_pragma(); }
76  omp_directive omp_comment TK_EOL
77  { pips_debug(1,"OMP pragma end\n"); step_directive_print($3); }
78 
79 | TK_STEP_PRAGMA
80  { pips_debug(1,"STEP pragma begin\n"); remove_old_pragma(); }
81  step_transformation TK_EOL
82  { pips_debug(1,"STEP pragma end\n"); }
83 ;
84 
85 step_transformation:
86  TK_STEP_TRANSFORMATION { pips_debug(1, "STEP transform : %d\n", $1); set_current_transform($1);}
87 ;
88 
89 omp_comment:
90 | TK_COMMENT { pips_debug(1,"COMMENT : %s\n", $1);}
91 ;
92 
93 omp_directive:
94  TK_OMP_PARALLEL
95  { $<step_directive>$ = begin_omp_construct($1, "PARALLEL"); }
96  omp_parallel_clauses
97  { $$ = concat_drt_clauses($<step_directive>2, $3); }
98 | TK_OMP_LOOP
99  { $<step_directive>$ = begin_omp_construct($1, "LOOP"); }
100  omp_loop_clauses
101  { $$ = concat_drt_clauses($<step_directive>2, $3); }
102 | TK_OMP_PARALLEL TK_OMP_LOOP
103  { $<step_directive>$=begin_omp_construct(STEP_PARALLEL_DO, "PARALLEL LOOP");}
104  omp_parallel_loop_clauses
105  { $$ = concat_drt_clauses($<step_directive>3, $4); }
106 | TK_OMP_MASTER
107  { $$ = begin_omp_construct($1, "MASTER");}
108 | TK_OMP_SINGLE
109  { $$ = begin_omp_construct($1, "SINGLE");}
110 | TK_OMP_BARRIER
111  { step_directive drt = begin_omp_construct($1, "BARRIER");
112  end_omp_construct($1);
113  $$ = drt; }
114 | TK_OMP_THREADPRIVATE
115  { $<step_directive>$ = begin_omp_construct($1, "THREADPRIVATE");}
116  omp_threadprivate_listvar
117  { $$ = concat_drt_clauses($<step_directive>2, $3);
118  end_omp_construct($1);}
119 | TK_OMP_END TK_OMP_PARALLEL
120  { $$ = end_omp_construct($2);}
121 | TK_OMP_END TK_OMP_LOOP omp_end_loop_clauses
122  { $$ = concat_drt_clauses(end_omp_construct($2), $3); }
123 | TK_OMP_END TK_OMP_PARALLEL TK_OMP_LOOP
124  { $$ = end_omp_construct(STEP_PARALLEL_DO);}
125 | TK_OMP_END TK_OMP_MASTER
126  { $$ = end_omp_construct($2);}
127 | TK_OMP_END TK_OMP_SINGLE
128  { $$ = end_omp_construct($2);}
129 ;
130 
131 omp_parallel_clauses: {$$=NIL;}
132 | omp_parallel_clauses omp_shared
133  { pips_debug(1, "clause SHARED\n");
134  $$ = CONS(STEP_CLAUSE, $2, $1); }
135 | omp_parallel_clauses omp_private
136  { pips_debug(1, "clause PRIVATE\n");
137  $$ = CONS(STEP_CLAUSE, $2, $1); }
138 | omp_parallel_clauses omp_copyin
139  { pips_debug(1, "clause COPYIN\n");
140  $$ = CONS(STEP_CLAUSE, $2, $1); }
141 | omp_parallel_clauses omp_firstprivate
142  { pips_debug(1, "clause FIRSTPRIVATE\n");
143  $$ = CONS(STEP_CLAUSE, $2, $1); }
144 | omp_parallel_clauses omp_reduction
145  { pips_debug(1, "clause REDUCTION\n");
146  $$ = CONS(STEP_CLAUSE, $2, $1); }
147 | omp_parallel_clauses omp_default
148  { pips_debug(1, "clause DEFAULT SKIPPED\n");
149  $$ = $1; }
150 ;
151 
152 
153 omp_loop_clauses: {$$=NIL;}
154 | omp_loop_clauses omp_private
155  { pips_debug(1, "clause PRIVATE\n");
156  $$ = CONS(STEP_CLAUSE, $2, $1);}
157 | omp_loop_clauses omp_firstprivate
158  { pips_debug(1, "clause FIRSTPRIVATE\n");
159  $$ = CONS(STEP_CLAUSE, $2, $1);}
160 | omp_loop_clauses omp_reduction
161  { pips_debug(1, "clause REDUCTION\n");
162  $$ = CONS(STEP_CLAUSE, $2, $1);}
163 | omp_loop_clauses omp_schedule
164  { pips_debug(1, "clause SCHEDULE\n");
165  $$ = CONS(STEP_CLAUSE, $2, $1);}
166 | omp_loop_clauses TK_OMP_NOWAIT
167  { pips_debug(1, "clause NOWAIT\n");
168  $$ = CONS(STEP_CLAUSE, make_step_clause_nowait(), $1);}
169 ;
170 
171 omp_end_loop_clauses:{$$=NIL;}
172 | omp_loop_clauses TK_OMP_NOWAIT {pips_debug(1, "clause NOWAIT\n");
173  $$ = CONS(STEP_CLAUSE, make_step_clause_nowait(), $1);}
174 ;
175 
176 omp_parallel_loop_clauses: { $$=NIL; }
177 | omp_parallel_loop_clauses omp_shared
178  { pips_debug(1, "clause SHARED\n");
179  $$ = CONS(STEP_CLAUSE, $2, $1); }
180 | omp_parallel_loop_clauses omp_private
181  { pips_debug(1, "clause PRIVATE\n");
182  $$ = CONS(STEP_CLAUSE, $2, $1); }
183 | omp_parallel_loop_clauses omp_copyin
184  { pips_debug(1, "clause COPYIN\n");
185  $$ = CONS(STEP_CLAUSE, $2, $1); }
186 | omp_parallel_loop_clauses omp_firstprivate
187  { pips_debug(1, "clause FIRSTPRIVATE\n");
188  $$ = CONS(STEP_CLAUSE, $2, $1); }
189 | omp_parallel_loop_clauses omp_reduction
190  { pips_debug(1, "clause REDUCTION\n");
191  $$ = CONS(STEP_CLAUSE, $2, $1); }
192 | omp_parallel_loop_clauses omp_schedule
193  { pips_debug(1, "clause SCHEDULE\n");
194  $$ = CONS(STEP_CLAUSE, $2, $1); }
195 | omp_parallel_loop_clauses omp_default
196  { pips_debug(1, "clause DEFAULT SKIPPED\n");
197  $$ = $1; }
198 ;
199 
200 omp_shared: TK_OMP_SHARED {pips_debug(2, "SHARED begin \n");}
201 TK_LPAREN ident_list TK_RPAREN {pips_debug(2, "SHARED end\n");
202  $$ = make_step_clause_shared($4);}
203 ;
204 
205 omp_private:
206  TK_OMP_PRIVATE
207  { pips_debug(2, "PRIVATE begin \n");}
208  TK_LPAREN ident_list TK_RPAREN
209  { pips_debug(2, "PRIVATE end\n");
210  $$ = make_step_clause_private($4);}
211 ;
212 
213 omp_copyin:
214  TK_OMP_COPYIN
215  { pips_debug(2, "COPYIN begin \n");}
216  TK_LPAREN ident_list TK_RPAREN
217  { pips_debug(2, "COPYIN end\n");
218  $$ = make_step_clause_copyin($4);}
219 ;
220 
221 omp_threadprivate_listvar :
222  { pips_debug(2, "THREADPRIVATE begin \n");}
223  TK_LPAREN ident_list TK_RPAREN
224  { pips_debug(2, "THREADPRIVATE end\n");
225  $$ = CONS(STEP_CLAUSE, make_step_clause_threadprivate($3),NIL);}
226 ;
227 
228 omp_firstprivate:
229  TK_OMP_FIRSTPRIVATE
230  { pips_debug(2, "FIRSTPRIVATE begin \n");}
231  TK_LPAREN ident_list TK_RPAREN
232  { pips_debug(2, "FIRSTPRIVATE end\n");
233  $$ = make_step_clause_firstprivate($4);}
234 ;
235 
236 omp_schedule:
237  TK_OMP_SCHEDULE
238  { pips_debug(2, "SCHEDULE begin \n");}
239  TK_LPAREN string_list TK_RPAREN
240  { pips_debug(2, "SCHEDULE end\n");
241  $$ = make_step_clause_schedule(gen_nreverse($4)); }
242 ;
243 
244 omp_reduction:
245  TK_OMP_REDUCTION
246  { pips_debug(2, "REDUCTION begin\n"); }
247  TK_LPAREN TK_OPERATOR
248  { pips_debug(2, "OPERATOR %d\n", $4); }
249  TK_COLON ident_list TK_RPAREN
250  { pips_debug(2, "REDUCTION end\n");
251  $$ = step_handle_reduction_clause($7, $4); }
252 ;
253 
254 omp_default:
255  TK_OMP_DEFAULT
256  { pips_debug(2, "DEFAULT begin\n"); }
257  TK_LPAREN TK_IDENT TK_RPAREN
258  { pips_debug(2, "DEFAULT %s end\n", $4); }
259 ;
260 
261 string_list:
262 TK_IDENT
263  { pips_debug(2, "first string %s\n", $1);
264  $$ = CONS(STRING, $1, NIL);
265  }
266 | string_list TK_COMMA TK_IDENT
267  { pips_debug(2, "next IDENT %s\n", $3);
268  $$ = CONS(STRING, $3, $1);
269  }
270 ;
271 
272 ident_list:
273  TK_IDENT
274  { pips_debug(2, "first IDENT %s\n", $1);
275  $$ = step_add_to_ident_list($1, NIL);
276  }
277 | ident_list TK_COMMA TK_IDENT
278  { pips_debug(2, "next IDENT %s\n", $3);
279  $$ = step_add_to_ident_list($3, $1);
280  }
281 ;
282 
283 %%
284 
285 static cons * step_add_to_ident_list(string name, cons * list)
286 {
287  pips_debug(2, "begin\n");
288  entity e = entity_from_user_name(name);
289  pips_assert("entity", !entity_undefined_p(e));
290  pips_debug(2, "end\n");
291  return CONS(ENTITY, e, list);
292 }
293 
294 static step_clause step_handle_reduction_clause(cons *ident_liste, int op)
295 {
296  pips_debug(2, "begin\n");
297  map_entity_int reductions = make_map_entity_int();
298  FOREACH(ENTITY, e, ident_liste)
299  {
300  pips_assert("first reduction", !bound_map_entity_int_p(reductions, e));
301  pips_debug(2,"add reduction %d variable : %s)\n", op, entity_name(e));
302  extend_map_entity_int(reductions, e, op);
303  }
304  gen_free_list(ident_liste);
305  pips_debug(2, "end\n");
306  return make_step_clause_reduction(reductions);
307 }
308 
309 static step_directive concat_drt_clauses(step_directive drt, cons *liste_clauses)
310 {
311  pips_debug(2, "begin\n");
312  step_directive_clauses(drt)= gen_nconc(liste_clauses, step_directive_clauses(drt));
313  pips_debug(2, "end\n");
314  return drt;
315 }
316 
317 void step_bison_parse(pragma pgm, statement stmt)
318 {
319  string pragma_str = pragma_string(pgm);
320 
321  if(pragma_str[strlen(pragma_str)-1]!= '\n')
322  pragma_str = strdup(concatenate(pragma_string(pgm),"\n",NULL));
323  else
324  pragma_str = strdup(pragma_string(pgm));
325 
326  pips_debug(1, "############### PARSER BEGIN ################\nwith :\n%s\non stmt : %p\n", pragma_str, stmt);
327  ifdebug(4)
328  yydebug=1;
329  pragma_str_original = strdup(pragma_str);
330  step_lexer_scan_string(pragma_str);
331  step_lexerparse();
332  free(pragma_str_original);
333  pips_debug(1, "############### PARSER END ################\n");
334 
335  free(pragma_str);
336 }