OpenADFortTk (including Open64 and OpenAnalysis references)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tester.cxx
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 // $Header: /Volumes/cvsrep/developer/OpenADFortTk/src/testers/tester.cxx,v 1.20 2005/03/31 03:44:33 utke Exp $
3 
4 #include <stdlib.h> // ANSI: cstdlib // for strtol
5 #include <string.h> // ANSI: cstring // for strcmp, etc.
6 #include <iostream>
7 
9 
11 #include "ir_reader.h" // fdump_tree
12 #include "w2f_driver.h" // W2F_*
15 
16 #include "ScalarizedRefTab.h"
17 #include "Diagnostics.h"
18 
19 #include "tester.h"
20 
21 using std::cerr;
22 using std::endl;
23 
24 static int
25 DumpExprTree(std::ostream& os, WN* wn);
26 
27 static int
28 DumpExprTree(std::ostream& os, OA::OA_ptr<OA::ExprTree> tree);
29 
30 static int
31 TestForEachPU(std::ostream& os, PU_Info* pu_forest);
32 
33 static int
34 TestForEachWNPU(std::ostream& os, WN* wn_pu);
35 
36 static int
37 TestForEachWN(std::ostream& os, WN* wn);
38 
39 static void
40 RecursiveFnWN(std::ostream& os, WN* wn);
41 
42 
43 int
44 whirltester::TestIR(std::ostream& os, PU_Info* pu_forest)
45 {
46  Diag_Set_Phase("WHIRL tester: TestIR");
47 
48  if (!pu_forest) { return 0; }
49 
50  // Here is where we can do something
51  TestForEachPU(os, pu_forest);
52 
53  return 0;
54 }
55 
56 static int
57 TestForEachPU(std::ostream& os, PU_Info* pu_forest)
58 {
59  Open64IRProcIterator procIt(pu_forest);
60  for ( ; procIt.isValid(); ++procIt) {
61 
62  // The PU_Info* for this PU
63  PU_Info* pu = (PU_Info*)procIt.current().hval();
64 
65  // The root of the WHIRL tree
66  WN* wn_pu = PU_Info_tree_ptr(pu);
67 
68  TestForEachWNPU(os, wn_pu);
69  }
70  return 0;
71 }
72 
73 static int
74 TestForEachWNPU(std::ostream& os, WN* wn_pu)
75 {
76  // We may want to do something special with the top level WN (FUNC_ENTRY)
77  //...
78 
79  // Iterate over each WN and do something
80  WN_TREE_CONTAINER<PRE_ORDER> wtree(wn_pu);
82 
83  for (it = wtree.begin(); it != wtree.end(); ++it) {
84  WN* curWN = it.Wn();
85 
86  TestForEachWN(os, curWN);
87  }
88 
89  // Alternatively we may want to recursively do something with the WHIRL tree
90  RecursiveFnWN(os, wn_pu);
91 
92  return 0;
93 }
94 
95 static int
96 TestForEachWN(std::ostream& os, WN* wn)
97 {
98  if (wn) {
99  DumpExprTree(os, wn);
100  os << endl;
101  }
102  return 0;
103 }
104 
105 static void
106 RecursiveFnWN(std::ostream& os, WN* wn)
107 {
108  if (wn == NULL) {
109  // Base case
110 
111  }
112 
113  OPERATOR opr = WN_operator(wn);
114 
115  if ( /* some test */ false ) {
116  // Base case
117  } else if (!OPERATOR_is_leaf(opr)) {
118 
119  // General recursive case
120  if (WN_operator(wn) == OPR_BLOCK) {
121  WN *kid = WN_first(wn);
122  while (kid) {
123  RecursiveFnWN(os, kid);
124  kid = WN_next(kid);
125  }
126  } else {
127  for (INT kidno = 0; kidno < WN_kid_count(wn); kidno++) {
128  WN* kid = WN_kid(wn, kidno);
129  RecursiveFnWN(os, kid);
130  }
131  }
132  }
133 }
134 
135 //****************************************************************************
136 
137 static int
138 DumpExprNode(std::ostream& os, OA::OA_ptr<OA::ExprTree::Node> node,
140 
141 static int
142 DumpExprTree(std::ostream& os, WN* wn)
143 {
145  ir = new Open64IRInterface;
146 
147  OPERATOR opr = WN_operator(wn);
148  if (OPERATOR_is_expression(opr)) {
151  DumpExprTree(os, tree);
152  }
153 
154  return 0;
155 }
156 
157 static int
158 DumpExprTree(std::ostream& os, OA::OA_ptr<OA::ExprTree> tree)
159 {
161  ir = new Open64IRInterface;
162 
163  OA::Tree::PreOrderIterator nodes_iter(*tree);
164  for ( ; nodes_iter.isValid(); ++nodes_iter) {
165  OA::OA_ptr<OA::Tree::Node> ntmp = nodes_iter.current();
167  DumpExprNode(os, node, ir);
168  }
169 
170  return 0;
171 }
172 
173 static int
176 {
177 #if 0
178  std::string& attr = node->getAttr();
179  os << "{ " << attr;
180 
181  if (node->isSym()) {
182  const char* nm = ir.toString(node->getSymHandle());
183  os << " sym: " << nm;
184  } else if (node->isConst()) {
185  os << " const";
186  }
187  os << " }";
188 #endif
189 
190  return 0;
191 }
192 
193 
194 //****************************************************************************
195 // TestIR_OA:
196 //****************************************************************************
197 
198 static int
199 TestIR_OA_ForEachWNPU(std::ostream& os, WN* wn_pu);
200 
201 #if 0
202 static void
203 TestIR_OA_ForEachVarRef(std::ostream& os, WN* wn,
204  Open64IRInterface& ir, UJNumbers& vnmap);
205 #endif
206 
207 int
208 whirltester::TestIR_OA(std::ostream& os, PU_Info* pu_forest)
209 {
210  Diag_Set_Phase("WHIRL tester: TestIR_OA");
211 
212  if (!pu_forest) { return 0; }
213 
214  Open64IRProcIterator procIt(pu_forest);
215  for ( ; procIt.isValid(); ++procIt) {
216 
217  // The PU_Info* for this PU
218  PU_Info* pu = (PU_Info*)procIt.current().hval();
219 
220  // The root of the WHIRL tree
221  WN* wn_pu = PU_Info_tree_ptr(pu);
222 
223  TestIR_OA_ForEachWNPU(os, wn_pu);
224  }
225  return 0;
226 }
227 
228 static int
229 TestIR_OA_ForEachWNPU(std::ostream& os, WN* wn_pu)
230 {
231  WN* fbody = WN_func_body(wn_pu);
232 
233 #if 0
234  Open64IRInterface irInterface;
235  Open64IRStmtIterator irStmtIter(fbody);
236  CFG cfg(irInterface, &irStmtIter, (SymHandle)WN_st(wn_pu), true);
237 
238  // Accumulate the ST* for parameters
239  std::set<SymHandle> params;
240  INT nparam = WN_num_formals(wn_pu);
241  for (int i = 0; i < nparam; ++i) {
242  ST* st = WN_st(WN_formal(wn_pu, i));
243  params.insert((SymHandle)st);
244  }
245 
246  UJNumbers vnmap(cfg, params);
247  TestIR_OA_ForEachVarRef(os, wn_pu, irInterface, vnmap);
248 #endif
249 
250  return 0;
251 }
252 
253 #if 0
254 static void
255 TestIR_OA_ForEachVarRef(std::ostream& os, WN* wn,
256  Open64IRInterface& ir, UJNumbers& vnmap)
257 {
258  if (wn == NULL) {
259  // Base case
260  }
261 
262  OPERATOR opr = WN_operator(wn);
263  // OPR_STID is a special case. It is a base case in the sense the
264  // it represents a LHS definition; but it is also a recursive case
265  // because uses are embedded in its RHS subtree.
266  bool varref = IsRefTranslatableToXAIF(wn);
267  bool recur = false;
268 
269  // Base case
270  if (varref || opr == OPR_STID) {
271  VN vn = vnmap.Find((ExprHandle)wn);
272 
273  ExprTree* tree = ir.GetExprTreeForExprHandle((ExprHandle)wn);
274  os << "VN = " << vn << endl;
275  os << " ";
276  DumpExprTree(os, tree);
277  os << endl;
278  delete tree;
279 
280  if (opr == OPR_STID) { recur = true; }
281  }
282 
283  // General recursive case
284  if ( (!varref || recur) && !OPERATOR_is_leaf(opr) ) {
285 
286  if (WN_operator(wn) == OPR_BLOCK) {
287  WN *kid = WN_first(wn);
288  while (kid) {
289  TestIR_OA_ForEachVarRef(os, kid, ir, vnmap);
290  kid = WN_next(kid);
291  }
292  } else {
293  for (INT kidno = 0; kidno < WN_kid_count(wn); ++kidno) {
294  WN* kid = WN_kid(wn, kidno);
295  TestIR_OA_ForEachVarRef(os, kid, ir, vnmap);
296  }
297  }
298  }
299 }
300 #endif
301 
302 
303 //****************************************************************************
304 // TestIR_whirl2f:
305 //****************************************************************************
306 
307 static WN*
308 PleaseGetMeSomething(WN* wn_pu);
309 
310 int
311 whirltester::TestIR_whirl2f(std::ostream& os, PU_Info* pu_forest)
312 {
313  static const int bufSZ = 1024 * 1024;
314  static char buf[bufSZ];
315 
316  Diag_Set_Phase("WHIRL tester: TestIR_whirl2f");
317  if (!pu_forest) { return 0; }
318 
320  W2F_Init();
321 
322  Open64IRProcIterator procIt(pu_forest);
323  for ( ; procIt.isValid(); ++procIt) {
324 
325  // The PU_Info* for this PU
326  PU_Info* pu = (PU_Info*)procIt.current().hval();
327 
328  // The root of the WHIRL tree and a statement to translate
329  WN* wn_pu = PU_Info_tree_ptr(pu);
330  WN* wn = PleaseGetMeSomething(wn_pu);
331 
333  W2F_Push_PU(wn_pu, wn);
334 
335  // Translate and output
336  os << "----> The tree:" << endl;
337  fdump_tree(stdout, wn);
338  os << endl;
339 
340  os << "----> The translation:" << endl;
341  W2F_Translate_Wn_Str(buf, bufSZ-1, wn);
342  os << buf << endl;
343 
344  W2F_Pop_PU();
346  break;
347  }
348 
349  W2F_Fini();
351  return 0;
352 }
353 
354 static WN*
356 {
357  WN_TREE_CONTAINER<PRE_ORDER> wtree(wn_pu);
359 
360  // Find the first non-empty block
361  WN* blk = NULL;
362  for (it = wtree.begin(); it != wtree.end(); ++it) {
363  WN* curWN = it.Wn();
364  OPERATOR opr = WN_operator(curWN);
365  if (opr == OPR_BLOCK && WN_first(curWN)) { // non-empty block
366  blk = curWN;
367  break;
368  }
369  }
370 
371  if (blk) {
372  // Create a comment and insert it into the block
373  WN* com1 = WN_CreateComment("$OpenAD BEGIN FRAGMENT(1)");
374  WN_INSERT_BlockFirst(blk, com1);
375 
376  WN* com2 = WN_CreateComment("$OpenAD END FRAGMENT(1)");
377  WN_INSERT_BlockLast(blk, com2);
378  }
379 
380  // Return body of the wn_pu
381  return WN_func_body(wn_pu); // blk;
382 }
383 
384 
385 //****************************************************************************