PIPS
treematch.c
Go to the documentation of this file.
1 /*
2 
3  $Id: treematch.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 #include "genC.h"
28 #include "linear.h"
29 #include "ri.h"
30 #include "effects.h"
31 
32 #include "resources.h"
33 #include "properties.h"
34 
35 #include "misc.h"
36 #include "ri-util.h"
37 #include "effects-util.h"
38 #include "pipsdbm.h"
39 
40 #include "sac.h"
41 #include "patterns.h"
42 #include <errno.h>
43 
44 
45 static matchTree patterns_tree = NULL;
47 {
48  pips_assert("tree match not already set",patterns_tree==NULL);
49  patterns_tree=t;
50 }
51 
53 {
54  pips_assert("tree match already set",patterns_tree!=NULL);
55  patterns_tree=NULL;
56 }
57 
59 static list curArgType = NIL;
60 
62 {
64  finalArgType = NIL;
65 
67  curArgType = NIL;
68 }
70 {
71  list c=NIL;
73  c= CONS(BASIC,basic_of_expression(arg),c);
74  return c;
75 }
76 
78 {
80 }
81 
83 {
85 }
86 
88 {
89  list pCurBas = curArgType;
90 
91  FOREACH(BASIC, finBas, finalArgType)
92  {
93  if(ENDP(pCurBas) )
94  return false;
95  basic curBas = BASIC(CAR(pCurBas));
96  if(!basic_equal_p(curBas, finBas))
97  return false;
98  pCurBas = CDR(pCurBas);
99  }
100 
102  curArgType = NIL;
103 
104  return true;
105 }
106 
107 
109 {
111  matchTree n = make_matchTree(NIL, sons);
112  return n;
113 }
114 
115 static void insert_tree_branch(matchTree t, int token, matchTree n)
116 {
117  extend_matchTreeSons(matchTree_sons(t), token, n);
118 }
119 
121 {
122  if (bound_matchTreeSons_p(matchTree_sons(t), token))
123  return apply_matchTreeSons(matchTree_sons(t), token);
124  else
125  return matchTree_undefined;
126 }
127 
128 static matchTree match_call(call , matchTree , list *);
130 {
131  syntax s = expression_syntax(arg);
132  switch(syntax_tag(s))
133  {
134  case is_syntax_call:
135  {
136  call c = syntax_call(s);
137  if (call_constant_p(c))
138  {
140  *args = CONS(EXPRESSION, arg, *args);
141  }
142  /* field call should be taken care of as references */
143  else if ( ENTITY_FIELD_P(call_function(c)) )
144  {
145  t=match_expression(binary_call_rhs(c),t,args);
146  /* right now we have matched the rhs of the field,
147  * now bring back the lhs !
148  */
149  if( !matchTree_undefined_p(t))
150  *REFCAR(*args) = (gen_chunkp)arg;
151  }
152  else
153  t = match_call(c, t, args);
154  break;
155  }
156 
157  case is_syntax_reference:
158  {
160  if(bas == basic_undefined || basic_type_size(bas)*8 >= get_int_property("SAC_SIMD_REGISTER_WIDTH"))
161  {
162  free_basic(bas);
163  return matchTree_undefined;
164  }
165  else
166  {
168  *args = CONS(EXPRESSION, arg, *args);
169  free_basic(bas);
170  }
171  break;
172  }
173 
174  case is_syntax_range:
175  default:
176  return matchTree_undefined; /* unexpected token !! -> no match */
177  }
178  return t;
179 }
180 
181 /* Warning: list of arguments is built in reversed order
182  * (the head is in fact the last argument) */
184 {
186  return matchTree_undefined; /* no match */
187 
189  if (matchTree_undefined_p(t))
190  return matchTree_undefined; /* no match */
191 
193  {
194  t=match_expression(arg,t,args);
195  if (matchTree_undefined_p(t) )
196  return matchTree_undefined;
197  }
198 
199 
200  return t;
201 }
202 
203 /* merge the 2 lists. Warning: list gets reversed. */
204 static list merge_lists(list l, list format)
205 {
206  list res = NIL;
207 
208  /* merge according to the format specifier list */
209  FOREACH(PATTERNARG,param,format)
210  {
212  {
213  if (l != NIL)
214  {
215  res = CONS(EXPRESSION, EXPRESSION(CAR(l)), res);
216  l = CDR(l);
217  }
218  else
219  pips_user_warning("Trying to use a malformed rule. Ignoring missing parameter...\n");
220  }
221  else
222  {
224 
225  res = CONS(EXPRESSION, e, res);
226  }
227  }
228 
229  /* append remaining arguments, if any */
230  for( ; l != NIL; l = CDR(l) )
231  res = CONS(EXPRESSION, EXPRESSION(CAR(l)), res);
232 
233  return res;
234 }
235 
236 /* return a list of matching statements */
238 {
239  list matches = NIL;
240 
241  if (statement_call_p(s))
242  {
243 
244  /* find the matching patterns */
245  list args = NIL;
247  if (matchTree_undefined_p(t))
248  {
249  gen_free_list(args);
250  return NIL;
251  }
252 
253  /* build the matches */
255  {
256  match m = make_match(
257  patternx_class(p),
258  merge_lists(args, patternx_args(p))
259  );
260  matches = CONS(MATCH, m, matches);
261  }
262  }
263 
264  return matches;
265 }
266 
267 void insert_opcodeClass(char * s, int nbArgs, list opcodes)
268 {
269  //Add the class and name in the map
270  make_opcodeClass(s, nbArgs, opcodes);
271 }
272 
273 static opcodeClass get_opcodeClass(char * s)
274 {
275  return gen_find_opcodeClass(s);
276 }
277 
278 void insert_pattern(char * s, list tokens, list args)
279 {
281  patternx p;
283 
284  if (c == opcodeClass_undefined)
285  {
286  pips_user_warning("defining pattern for an undefined opcodeClass (%s).",s);
287  return;
288  }
289 
290  p = make_patternx(c, args);
291 
292  for( ; tokens != NIL; tokens = CDR(tokens) )
293  {
294  int token = INT(CAR(tokens));
295  matchTree next = select_tree_branch(m, token);
296 
297  if (next == matchTree_undefined)
298  {
299  /* no such branch -> create a new branch */
300  next = make_tree();
301  insert_tree_branch(m, token, next);
302  }
303 
304  m = next;
305  }
306 
308 }
309 
310 bool simd_treematcher(const string module_name)
311 {
313  /* create a new tree match */
314  matchTree treematch = make_tree();
315  set_simd_treematch(treematch);
316 
317  /* fill it */
318  sac_lineno=0;
319  patterns_yyin=fopen_config("patterns.def","SIMD_PATTERN_FILE",NULL);
321  fclose(patterns_yyin);
322 
323  /* put it in pipsdbm */
324  DB_PUT_MEMORY_RESOURCE(DBR_SIMD_TREEMATCH,"",treematch);
325 
326  /* clean up */
328  return true;
329 }
int get_int_property(const string)
void free_basic(basic p)
Definition: ri.c:107
match make_match(opcodeClass a1, list a2)
Definition: sac_private.c:153
matchTree make_matchTree(list a1, matchTreeSons a2)
Definition: sac_private.c:54
matchTree apply_matchTreeSons(matchTreeSons f, intptr_t k)
Definition: sac_private.c:99
void extend_matchTreeSons(matchTreeSons f, intptr_t k, matchTree v)
Definition: sac_private.c:105
bool bound_matchTreeSons_p(matchTreeSons f, intptr_t k)
Definition: sac_private.c:111
patternx make_patternx(opcodeClass a1, list a2)
Definition: sac_private.c:334
opcodeClass make_opcodeClass(string a1, intptr_t a2, list a3)
Definition: sac_private.c:468
matchTreeSons make_matchTreeSons(void)
Definition: sac_private.c:96
opcodeClass gen_find_opcodeClass(char *s)
Definition: sac_private.c:454
size_t sac_lineno
patterns.c
Definition: patterns.c:97
@ REFERENCE_TOK
UNKNOWN_TOK
Definition: patterns.h:58
@ CONSTANT_TOK
LOG_REF_TOK
Definition: patterns.h:68
int patterns_yyparse(void)
@ INT
Definition: atomic.c:48
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
FILE * fopen_config(const char *canonical_name, const char *cproperty, const char *cenv)
Definition: file.c:952
#define call_constant_p(C)
Definition: flint_check.c:51
union gen_chunk * gen_chunkp
void gen_full_free_list(list l)
Definition: genClib.c:1023
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define REFCAR(pc)
Get the adress of the first element of a list.
Definition: newgen_list.h:119
#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
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
#define DB_PUT_MEMORY_RESOURCE(res_name, own_name, res_val)
conform to old interface.
Definition: pipsdbm-local.h:66
call statement_call(statement)
Get the call of a statement.
Definition: statement.c:1406
bool statement_call_p(statement)
Definition: statement.c:364
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
int get_operator_id(entity e)
Definition: operatorid.c:161
#define binary_call_rhs(c)
#define ENTITY_FIELD_P(e)
C data structure and pointer management.
bool top_level_entity_p(entity e)
Check if the scope of entity e is global.
Definition: entity.c:1130
expression int_to_expression(_int i)
transform an int into an expression and generate the corresponding entity if necessary; it is not cle...
Definition: expression.c:1188
basic basic_of_expression(expression)
basic basic_of_expression(expression exp): Makes a basic of the same basic as the expression "exp".
Definition: type.c:1383
int basic_type_size(basic)
See also SizeOfElements()
Definition: type.c:1074
bool basic_equal_p(basic, basic)
Definition: type.c:927
basic basic_of_reference(reference)
Retrieves the basic of a reference in a newly allocated basic object.
Definition: type.c:1459
#define syntax_reference(x)
Definition: ri.h:2730
#define syntax_tag(x)
Definition: ri.h:2727
#define call_function(x)
Definition: ri.h:709
@ is_syntax_range
Definition: ri.h:2692
@ is_syntax_call
Definition: ri.h:2693
@ is_syntax_reference
Definition: ri.h:2691
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define basic_undefined
Definition: ri.h:556
#define syntax_call(x)
Definition: ri.h:2736
#define call_arguments(x)
Definition: ri.h:711
#define expression_syntax(x)
Definition: ri.h:1247
#define BASIC(x)
BASIC.
Definition: ri.h:550
FILE * patterns_yyin
symbols exported by the parser
Definition: sac.h:157
#define matchTree_undefined
Definition: sac_private.h:150
#define MATCH(x)
MATCH.
Definition: sac_private.h:221
#define opcodeClass_undefined
Definition: sac_private.h:507
#define patternArg_dynamic_p(x)
Definition: sac_private.h:378
#define matchTree_undefined_p(x)
Definition: sac_private.h:151
#define PATTERNX(x)
PATTERNX.
Definition: sac_private.h:384
#define matchTree_sons(x)
Definition: sac_private.h:176
#define matchTree_patterns(x)
Definition: sac_private.h:174
#define patternx_class(x)
Definition: sac_private.h:414
#define patternArg_static(x)
Definition: sac_private.h:377
#define PATTERNARG(x)
PATTERNARG.
Definition: sac_private.h:335
#define patternx_args(x)
Definition: sac_private.h:416
static hash_table matches
Definition: simdizer.c:60
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: replace.c:135
static matchTree select_tree_branch(matchTree t, int token)
Definition: treematch.c:120
void simd_reset_finalArgType()
Definition: treematch.c:61
void reset_simd_treematch()
Definition: treematch.c:52
static matchTree patterns_tree
Definition: treematch.c:45
void simd_fill_curArgType(statement stat)
Definition: treematch.c:82
static matchTree match_call(call, matchTree, list *)
Warning: list of arguments is built in reversed order (the head is in fact the last argument)
Definition: treematch.c:183
static matchTree match_expression(expression arg, matchTree t, list *args)
Definition: treematch.c:129
bool simd_treematcher(const string module_name)
Definition: treematch.c:310
bool simd_check_argType()
Definition: treematch.c:87
list match_statement(statement s)
return a list of matching statements
Definition: treematch.c:237
static void insert_tree_branch(matchTree t, int token, matchTree n)
Definition: treematch.c:115
void insert_pattern(char *s, list tokens, list args)
Definition: treematch.c:278
void set_simd_treematch(matchTree t)
treematch.c
Definition: treematch.c:46
static list simd_fill_curArgType_call(call ca)
Definition: treematch.c:69
static opcodeClass get_opcodeClass(char *s)
Definition: treematch.c:273
static matchTree make_tree()
Definition: treematch.c:108
static list curArgType
Definition: treematch.c:59
static list finalArgType
Definition: treematch.c:58
void insert_opcodeClass(char *s, int nbArgs, list opcodes)
Definition: treematch.c:267
static list merge_lists(list l, list format)
merge the 2 lists.
Definition: treematch.c:204
void simd_fill_finalArgType(statement stat)
Definition: treematch.c:77