]> sourceware.org Git - systemtap.git/blame - elaborate.cxx
PR9866 band-aid: deactivate new CONFIG_STACKTRACE code for older kernels
[systemtap.git] / elaborate.cxx
CommitLineData
2b066ec1 1// elaboration functions
482fe2af 2// Copyright (C) 2005-2009 Red Hat Inc.
1773fd03 3// Copyright (C) 2008 Intel Corporation
69c68955
FCE
4//
5// This file is part of systemtap, and is free software. You can
6// redistribute it and/or modify it under the terms of the GNU General
7// Public License (GPL); either version 2, or (at your option) any
8// later version.
2b066ec1
FCE
9
10#include "config.h"
11#include "elaborate.h"
12#include "parse.h"
20c6c071 13#include "tapsets.h"
dc38c0ae 14#include "session.h"
72dbc915 15#include "util.h"
2b066ec1
FCE
16
17extern "C" {
18#include <sys/utsname.h>
a477f3f1 19#include <fnmatch.h>
2b066ec1
FCE
20}
21
2b066ec1 22#include <algorithm>
67c0a579
GH
23#include <fstream>
24#include <map>
29e64872 25#include <cassert>
67c0a579
GH
26#include <set>
27#include <vector>
1b07c728
FCE
28#include <algorithm>
29#include <iterator>
30
2b066ec1 31
2b066ec1
FCE
32using namespace std;
33
34
35// ------------------------------------------------------------------------
36
5d23847d
FCE
37// Used in probe_point condition construction. Either argument may be
38// NULL; if both, return NULL too. Resulting expression is a deep
39// copy for symbol resolution purposes.
40expression* add_condition (expression* a, expression* b)
41{
42 if (!a && !b) return 0;
43 if (! a) return deep_copy_visitor::deep_copy(b);
44 if (! b) return deep_copy_visitor::deep_copy(a);
45 logical_and_expr la;
46 la.op = "&&";
47 la.left = a;
48 la.right = b;
49 la.tok = a->tok; // or could be b->tok
50 return deep_copy_visitor::deep_copy(& la);
51}
52
53// ------------------------------------------------------------------------
54
55
2b066ec1
FCE
56
57derived_probe::derived_probe (probe *p):
58 base (p)
59{
5d23847d
FCE
60 assert (p);
61 this->locations = p->locations;
62 this->tok = p->tok;
63 this->privileged = p->privileged;
64 this->body = deep_copy_visitor::deep_copy(p->body);
2b066ec1
FCE
65}
66
67
68derived_probe::derived_probe (probe *p, probe_point *l):
69 base (p)
70{
5d23847d
FCE
71 assert (p);
72 this->tok = p->tok;
73 this->privileged = p->privileged;
74 this->body = deep_copy_visitor::deep_copy(p->body);
cbbe8080 75
5d23847d
FCE
76 assert (l);
77 this->locations.push_back (l);
2b066ec1
FCE
78}
79
98afd80e 80
b8da0ad1
FCE
81void
82derived_probe::printsig (ostream& o) const
83{
84 probe::printsig (o);
85 printsig_nested (o);
86}
87
88void
89derived_probe::printsig_nested (ostream& o) const
90{
91 // We'd like to enclose the probe derivation chain in a /* */
92 // comment delimiter. But just printing /* base->printsig() */ is
93 // not enough, since base might itself be a derived_probe. So we,
94 // er, "cleverly" encode our nesting state as a formatting flag for
95 // the ostream.
96 ios::fmtflags f = o.flags (ios::internal);
97 if (f & ios::internal)
98 {
99 // already nested
100 o << " <- ";
101 base->printsig (o);
102 }
103 else
104 {
105 // outermost nesting
106 o << " /* <- ";
107 base->printsig (o);
108 o << " */";
109 }
110 // restore flags
111 (void) o.flags (f);
112}
113
114
c3a3c0c9 115void
2c5a19c6 116derived_probe::collect_derivation_chain (std::vector<probe*> &probes_list)
c3a3c0c9
WC
117{
118 probes_list.push_back(this);
119 base->collect_derivation_chain(probes_list);
120}
121
122
b20febf3 123probe_point*
b8da0ad1 124derived_probe::sole_location () const
dc38c0ae 125{
b20febf3
FCE
126 if (locations.size() == 0)
127 throw semantic_error ("derived_probe with no locations", this->tok);
128 else if (locations.size() > 1)
129 throw semantic_error ("derived_probe with too many locations", this->tok);
57148ee7 130 else
b20febf3 131 return locations[0];
dc38c0ae
DS
132}
133
134
47dd066d 135
20c6c071 136// ------------------------------------------------------------------------
98afd80e
FCE
137// Members of derived_probe_builder
138
139bool
140derived_probe_builder::get_param (std::map<std::string, literal*> const & params,
141 const std::string& key,
142 std::string& value)
143{
144 map<string, literal *>::const_iterator i = params.find (key);
145 if (i == params.end())
146 return false;
147 literal_string * ls = dynamic_cast<literal_string *>(i->second);
148 if (!ls)
149 return false;
150 value = ls->value;
151 return true;
152}
153
20c6c071 154
98afd80e
FCE
155bool
156derived_probe_builder::get_param (std::map<std::string, literal*> const & params,
157 const std::string& key,
158 int64_t& value)
159{
160 map<string, literal *>::const_iterator i = params.find (key);
161 if (i == params.end())
162 return false;
163 if (i->second == NULL)
164 return false;
165 literal_number * ln = dynamic_cast<literal_number *>(i->second);
166 if (!ln)
167 return false;
168 value = ln->value;
169 return true;
170}
171
172
888af770
FCE
173bool
174derived_probe_builder::has_null_param (std::map<std::string, literal*> const & params,
175 const std::string& key)
176{
177 map<string, literal *>::const_iterator i = params.find(key);
178 return (i != params.end() && i->second == NULL);
179}
180
181
98afd80e
FCE
182
183// ------------------------------------------------------------------------
20c6c071
GH
184// Members of match_key.
185
57148ee7
FCE
186match_key::match_key(string const & n)
187 : name(n),
188 have_parameter(false),
5d23847d 189 parameter_type(pe_unknown)
20c6c071
GH
190{
191}
192
193match_key::match_key(probe_point::component const & c)
194 : name(c.functor),
195 have_parameter(c.arg != NULL),
5d23847d 196 parameter_type(c.arg ? c.arg->type : pe_unknown)
20c6c071
GH
197{
198}
199
200match_key &
57148ee7 201match_key::with_number()
20c6c071
GH
202{
203 have_parameter = true;
5d23847d 204 parameter_type = pe_long;
20c6c071
GH
205 return *this;
206}
207
208match_key &
57148ee7 209match_key::with_string()
20c6c071
GH
210{
211 have_parameter = true;
5d23847d 212 parameter_type = pe_string;
20c6c071
GH
213 return *this;
214}
215
57148ee7 216string
20c6c071
GH
217match_key::str() const
218{
219 if (have_parameter)
220 switch (parameter_type)
221 {
5d23847d
FCE
222 case pe_string: return name + "(string)";
223 case pe_long: return name + "(number)";
20c6c071
GH
224 default: return name + "(...)";
225 }
226 return name;
227}
228
57148ee7 229bool
20c6c071
GH
230match_key::operator<(match_key const & other) const
231{
232 return ((name < other.name)
57148ee7
FCE
233
234 || (name == other.name
20c6c071 235 && have_parameter < other.have_parameter)
57148ee7
FCE
236
237 || (name == other.name
238 && have_parameter == other.have_parameter
20c6c071
GH
239 && parameter_type < other.parameter_type));
240}
2b066ec1 241
a477f3f1
DS
242static bool
243isglob(string const & str)
244{
245 return(str.find('*') != str.npos);
246}
247
248bool
249match_key::globmatch(match_key const & other) const
250{
251 const char *other_str = other.name.c_str();
252 const char *name_str = name.c_str();
253
254 return ((fnmatch(name_str, other_str, FNM_NOESCAPE) == 0)
57148ee7 255 && have_parameter == other.have_parameter
a477f3f1
DS
256 && parameter_type == other.parameter_type);
257}
258
2b066ec1 259// ------------------------------------------------------------------------
20c6c071
GH
260// Members of match_node
261// ------------------------------------------------------------------------
262
263match_node::match_node()
264 : end(NULL)
265{}
266
267match_node *
57148ee7 268match_node::bind(match_key const & k)
20c6c071 269{
b4ceace2
FCE
270 if (k.name == "*")
271 throw semantic_error("invalid use of wildcard probe point component");
272
20c6c071
GH
273 map<match_key, match_node *>::const_iterator i = sub.find(k);
274 if (i != sub.end())
275 return i->second;
276 match_node * n = new match_node();
277 sub.insert(make_pair(k, n));
278 return n;
279}
280
57148ee7 281void
20c6c071
GH
282match_node::bind(derived_probe_builder * e)
283{
284 if (end)
59bafbe8 285 throw semantic_error("duplicate probe point pattern");
20c6c071
GH
286 end = e;
287}
288
57148ee7 289match_node *
20c6c071
GH
290match_node::bind(string const & k)
291{
292 return bind(match_key(k));
293}
294
295match_node *
296match_node::bind_str(string const & k)
297{
298 return bind(match_key(k).with_string());
299}
300
57148ee7 301match_node *
20c6c071
GH
302match_node::bind_num(string const & k)
303{
304 return bind(match_key(k).with_number());
305}
306
b4ceace2
FCE
307
308void
309match_node::find_and_build (systemtap_session& s,
310 probe* p, probe_point *loc, unsigned pos,
311 vector<derived_probe *>& results)
20c6c071 312{
b4ceace2 313 assert (pos <= loc->components.size());
57148ee7 314 if (pos == loc->components.size()) // matched all probe point components so far
20c6c071 315 {
b4ceace2
FCE
316 derived_probe_builder *b = end; // may be 0 if only nested names are bound
317
318 if (! b)
319 {
320 string alternatives;
321 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
322 alternatives += string(" ") + i->first.str();
323
57148ee7 324 throw semantic_error (string("probe point truncated at position ") +
b4ceace2 325 lex_cast<string> (pos) +
cfd621bc 326 " (follow:" + alternatives + ")", loc->tok);
b4ceace2
FCE
327 }
328
329 map<string, literal *> param_map;
330 for (unsigned i=0; i<pos; i++)
331 param_map[loc->components[i]->functor] = loc->components[i]->arg;
332 // maybe 0
333
334 b->build (s, p, loc, param_map, results);
20c6c071 335 }
a477f3f1 336 else if (isglob(loc->components[pos]->functor)) // wildcard?
20c6c071 337 {
a477f3f1
DS
338 match_key match (* loc->components[pos]);
339
828c3ed5
DS
340 // Call find_and_build for each possible match. Ignore errors -
341 // unless we don't find any match.
342 unsigned int num_results = results.size();
b4ceace2
FCE
343 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
344 {
a477f3f1 345 const match_key& subkey = i->first;
828c3ed5 346 match_node* subnode = i->second;
a477f3f1 347
49abf162
FCE
348 if (pending_interrupts) break;
349
a477f3f1 350 if (match.globmatch(subkey))
828c3ed5 351 {
ddfc759e
DS
352 if (s.verbose > 2)
353 clog << "wildcard '" << loc->components[pos]->functor
354 << "' matched '" << subkey.name << "'" << endl;
355
356 // When we have a wildcard, we need to create a copy of
357 // the probe point. Then we'll create a copy of the
358 // wildcard component, and substitute the non-wildcard
359 // functor.
360 probe_point *non_wildcard_pp = new probe_point(*loc);
361 probe_point::component *non_wildcard_component
362 = new probe_point::component(*loc->components[pos]);
363 non_wildcard_component->functor = subkey.name;
364 non_wildcard_pp->components[pos] = non_wildcard_component;
365
5d23847d
FCE
366 // NB: probe conditions are not attached at the wildcard
367 // (component/functor) level, but at the overall
368 // probe_point level.
369
ddfc759e 370 // recurse (with the non-wildcard probe point)
a477f3f1
DS
371 try
372 {
ddfc759e
DS
373 subnode->find_and_build (s, p, non_wildcard_pp, pos+1,
374 results);
a477f3f1
DS
375 }
376 catch (const semantic_error& e)
377 {
378 // Ignore semantic_errors while expanding wildcards.
379 // If we get done and nothing was expanded, the code
380 // following the loop will complain.
ddfc759e
DS
381
382 // If this wildcard didn't match, cleanup.
383 delete non_wildcard_pp;
384 delete non_wildcard_component;
a477f3f1 385 }
828c3ed5 386 }
a477f3f1 387 }
cedd10f4 388 if (! loc->optional && num_results == results.size())
828c3ed5
DS
389 {
390 // We didn't find any wildcard matches (since the size of
391 // the result vector didn't change). Throw an error.
392 string alternatives;
393 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
394 alternatives += string(" ") + i->first.str();
57148ee7 395
828c3ed5
DS
396 throw semantic_error(string("probe point mismatch at position ") +
397 lex_cast<string> (pos) +
a8cf6a45
MW
398 " (alternatives:" + alternatives + ")" +
399 " didn't find any wildcard matches",
cfd621bc 400 loc->tok);
828c3ed5 401 }
b4ceace2 402 }
57148ee7 403 else
20c6c071 404 {
b4ceace2
FCE
405 match_key match (* loc->components[pos]);
406 sub_map_iterator_t i = sub.find (match);
407 if (i == sub.end()) // no match
408 {
409 string alternatives;
410 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
411 alternatives += string(" ") + i->first.str();
57148ee7
FCE
412
413 throw semantic_error (string("probe point mismatch at position ") +
b4ceace2 414 lex_cast<string> (pos) +
cfd621bc
FCE
415 " (alternatives:" + alternatives + ")",
416 loc->tok);
b4ceace2
FCE
417 }
418
419 match_node* subnode = i->second;
420 // recurse
421 subnode->find_and_build (s, p, loc, pos+1, results);
20c6c071
GH
422 }
423}
424
b4ceace2 425
aa30ccd3
FCE
426void
427match_node::build_no_more (systemtap_session& s)
428{
429 for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
430 i->second->build_no_more (s);
431 if (end) end->build_no_more (s);
432}
433
434
20c6c071
GH
435// ------------------------------------------------------------------------
436// Alias probes
437// ------------------------------------------------------------------------
438
c1d5f3f6
FCE
439struct alias_derived_probe: public derived_probe
440{
2c5a19c6
DB
441 alias_derived_probe (probe* base, probe_point *l, const probe_alias *a):
442 derived_probe (base, l), alias(a) {}
c1d5f3f6 443
b20febf3
FCE
444 void upchuck () { throw semantic_error ("inappropriate", this->tok); }
445
446 // Alias probes are immediately expanded to other derived_probe
447 // types, and are not themselves emitted or listed in
448 // systemtap_session.probes
dc38c0ae 449
78f6bba6 450 void join_group (systemtap_session&) { upchuck (); }
2c5a19c6
DB
451
452 virtual const probe_alias *get_alias () const { return alias; }
453
454private:
455 const probe_alias *alias; // Used to check for recursion
c1d5f3f6
FCE
456};
457
458
20c6c071 459struct
57148ee7 460alias_expansion_builder
20c6c071
GH
461 : public derived_probe_builder
462{
463 probe_alias * alias;
464
57148ee7 465 alias_expansion_builder(probe_alias * a)
20c6c071
GH
466 : alias(a)
467 {}
468
5227f1ea 469 virtual void build(systemtap_session & sess,
57148ee7 470 probe * use,
20c6c071 471 probe_point * location,
78f6bba6 472 std::map<std::string, literal *> const &,
20c6c071
GH
473 vector<derived_probe *> & finished_results)
474 {
2c5a19c6
DB
475 // Don't build the alias expansion if infinite recursion is detected.
476 if (checkForRecursiveExpansion (use)) {
477 stringstream msg;
478 msg << "Recursive loop in alias expansion of " << *location << " at " << location->tok->location;
479 // semantic_errors thrown here are ignored.
480 sess.print_error (semantic_error (msg.str()));
481 return;
482 }
483
20c6c071 484 // We're going to build a new probe and wrap it up in an
c8959a29 485 // alias_expansion_probe so that the expansion loop recognizes it as
20c6c071 486 // such and re-expands its expansion.
57148ee7 487
2c5a19c6 488 alias_derived_probe * n = new alias_derived_probe (use, location /* soon overwritten */, this->alias);
20c6c071
GH
489 n->body = new block();
490
5d23847d 491 // The new probe gets the location list of the alias (with incoming condition joined)
20c6c071 492 n->locations = alias->locations;
5d23847d
FCE
493 for (unsigned i=0; i<n->locations.size(); i++)
494 n->locations[i]->condition = add_condition (n->locations[i]->condition,
495 location->condition);
496
c3a3c0c9
WC
497 // the token location of the alias,
498 n->tok = location->tok;
20c6c071 499
5227f1ea 500 // and statements representing the concatenation of the alias'
57148ee7 501 // body with the use's.
5227f1ea
GH
502 //
503 // NB: locals are *not* copied forward, from either alias or
504 // use. The expansion should have its locals re-inferred since
505 // there's concatenated code here and we only want one vardecl per
506 // resulting variable.
507
cedd10f4 508 if (alias->epilogue_style)
ba6f838d 509 n->body = new block (use->body, alias->body);
cedd10f4 510 else
ba6f838d 511 n->body = new block (alias->body, use->body);
57148ee7 512
cedd10f4 513 derive_probes (sess, n, finished_results, location->optional);
20c6c071 514 }
2c5a19c6
DB
515
516 bool checkForRecursiveExpansion (probe *use)
517 {
518 // Collect the derivation chain of this probe.
519 vector<probe*>derivations;
520 use->collect_derivation_chain (derivations);
521
522 // Check all probe points in the alias expansion against the currently-being-expanded probe point
523 // of each of the probes in the derivation chain, looking for a match. This
524 // indicates infinite recursion.
525 // The first element of the derivation chain will be the derived_probe representing 'use', so
526 // start the search with the second element.
527 assert (derivations.size() > 0);
528 assert (derivations[0] == use);
529 for (unsigned d = 1; d < derivations.size(); ++d) {
530 if (use->get_alias() == derivations[d]->get_alias())
531 return true; // recursion detected
532 }
533 return false;
534 }
20c6c071
GH
535};
536
537
538// ------------------------------------------------------------------------
539// Pattern matching
540// ------------------------------------------------------------------------
541
542
543// Register all the aliases we've seen in library files, and the user
544// file, as patterns.
545
546void
547systemtap_session::register_library_aliases()
548{
549 vector<stapfile*> files(library_files);
550 files.push_back(user_file);
551
552 for (unsigned f = 0; f < files.size(); ++f)
553 {
554 stapfile * file = files[f];
555 for (unsigned a = 0; a < file->aliases.size(); ++a)
556 {
557 probe_alias * alias = file->aliases[a];
57148ee7 558 try
59bafbe8
FCE
559 {
560 for (unsigned n = 0; n < alias->alias_names.size(); ++n)
561 {
562 probe_point * name = alias->alias_names[n];
563 match_node * n = pattern_root;
564 for (unsigned c = 0; c < name->components.size(); ++c)
565 {
566 probe_point::component * comp = name->components[c];
567 // XXX: alias parameters
568 if (comp->arg)
57148ee7
FCE
569 throw semantic_error("alias component "
570 + comp->functor
59bafbe8
FCE
571 + " contains illegal parameter");
572 n = n->bind(comp->functor);
573 }
574 n->bind(new alias_expansion_builder(alias));
575 }
576 }
577 catch (const semantic_error& e)
578 {
7e41d3dc
FCE
579 semantic_error* er = new semantic_error (e); // copy it
580 stringstream msg;
581 msg << e.msg2;
582 msg << " while registering probe alias ";
583 alias->printsig(msg);
584 er->msg2 = msg.str();
585 print_error (* er);
586 delete er;
59bafbe8 587 }
20c6c071
GH
588 }
589 }
590}
591
592
5227f1ea
GH
593static unsigned max_recursion = 100;
594
57148ee7 595struct
5227f1ea
GH
596recursion_guard
597{
598 unsigned & i;
599 recursion_guard(unsigned & i) : i(i)
600 {
601 if (i > max_recursion)
602 throw semantic_error("recursion limit reached");
603 ++i;
604 }
57148ee7 605 ~recursion_guard()
5227f1ea
GH
606 {
607 --i;
608 }
609};
610
20c6c071
GH
611// The match-and-expand loop.
612void
b4ceace2
FCE
613derive_probes (systemtap_session& s,
614 probe *p, vector<derived_probe*>& dps,
cedd10f4 615 bool optional)
20c6c071
GH
616{
617 for (unsigned i = 0; i < p->locations.size(); ++i)
618 {
49abf162
FCE
619 if (pending_interrupts) break;
620
20c6c071 621 probe_point *loc = p->locations[i];
a971b891 622
fe3d01fa
FCE
623 try
624 {
fe3d01fa 625 unsigned num_atbegin = dps.size();
d898100a
FCE
626
627 // Pass down optional flag from e.g. alias reference to each
628 // probe_point instance. We do this by temporarily overriding
629 // the probe_point optional flag. We could instead deep-copy
630 // and set a flag on the copy permanently.
631 bool old_loc_opt = loc->optional;
632 loc->optional = loc->optional || optional;
633 s.pattern_root->find_and_build (s, p, loc, 0, dps); // <-- actual derivation!
634 loc->optional = old_loc_opt;
fe3d01fa 635 unsigned num_atend = dps.size();
d898100a
FCE
636
637 if (! (loc->optional||optional) && // something required, but
cedd10f4 638 num_atbegin == num_atend) // nothing new derived!
d898100a
FCE
639 throw semantic_error ("no match");
640
641 if (loc->sufficient && (num_atend > num_atbegin))
642 {
643 if (s.verbose > 1)
644 {
645 clog << "Probe point ";
646 p->locations[i]->print(clog);
647 clog << " sufficient, skipped";
648 for (unsigned j = i+1; j < p->locations.size(); ++j)
649 {
650 clog << " ";
651 p->locations[j]->print(clog);
652 }
653 clog << endl;
654 }
655 break; // we need not try to derive for any other locations
656 }
fe3d01fa
FCE
657 }
658 catch (const semantic_error& e)
659 {
b4ceace2 660 // XXX: prefer not to print_error at every nest/unroll level
7e41d3dc
FCE
661
662 semantic_error* er = new semantic_error (e); // copy it
663 stringstream msg;
664 msg << e.msg2;
665 msg << " while resolving probe point " << *loc;
666 er->msg2 = msg.str();
667 s.print_error (* er);
668 delete er;
fe3d01fa 669 }
a971b891 670
20c6c071
GH
671 }
672}
673
b4ceace2
FCE
674
675
20c6c071 676// ------------------------------------------------------------------------
67c0a579 677//
d02548c0 678// Indexable usage checks
67c0a579
GH
679//
680
d02548c0 681struct symbol_fetcher
07c17d67 682 : public throwing_visitor
67c0a579 683{
d02548c0 684 symbol *&sym;
67c0a579 685
57148ee7 686 symbol_fetcher (symbol *&sym): sym(sym)
67c0a579
GH
687 {}
688
d02548c0
GH
689 void visit_symbol (symbol* e)
690 {
691 sym = e;
692 }
693
b0be9bdb
FCE
694 void visit_target_symbol (target_symbol* e)
695 {
696 sym = e;
697 }
698
d02548c0
GH
699 void visit_arrayindex (arrayindex* e)
700 {
701 e->base->visit_indexable (this);
702 }
703
9b5af295
JS
704 void visit_cast_op (cast_op* e)
705 {
706 sym = e;
707 }
708
d02548c0
GH
709 void throwone (const token* t)
710 {
711 throw semantic_error ("Expecting symbol or array index expression", t);
712 }
713};
714
07c17d67 715symbol *
d02548c0
GH
716get_symbol_within_expression (expression *e)
717{
718 symbol *sym = NULL;
719 symbol_fetcher fetcher(sym);
720 e->visit (&fetcher);
b0be9bdb 721 return sym; // NB: may be null!
d02548c0
GH
722}
723
724static symbol *
725get_symbol_within_indexable (indexable *ix)
726{
727 symbol *array = NULL;
728 hist_op *hist = NULL;
729 classify_indexable(ix, array, hist);
730 if (array)
731 return array;
732 else
733 return get_symbol_within_expression (hist->stat);
734}
735
736struct mutated_var_collector
07c17d67 737 : public traversing_visitor
d02548c0
GH
738{
739 set<vardecl *> * mutated_vars;
740
57148ee7 741 mutated_var_collector (set<vardecl *> * mm)
d02548c0
GH
742 : mutated_vars (mm)
743 {}
744
745 void visit_assignment(assignment* e)
746 {
747 if (e->type == pe_stats && e->op == "<<<")
748 {
749 vardecl *vd = get_symbol_within_expression (e->left)->referent;
750 if (vd)
751 mutated_vars->insert (vd);
752 }
1bbeef03 753 traversing_visitor::visit_assignment(e);
d02548c0
GH
754 }
755
67c0a579
GH
756 void visit_arrayindex (arrayindex *e)
757 {
d02548c0
GH
758 if (is_active_lvalue (e))
759 {
760 symbol *sym;
761 if (e->base->is_symbol (sym))
762 mutated_vars->insert (sym->referent);
763 else
764 throw semantic_error("Assignment to read-only histogram bucket", e->tok);
765 }
1bbeef03 766 traversing_visitor::visit_arrayindex (e);
67c0a579
GH
767 }
768};
769
770
d02548c0 771struct no_var_mutation_during_iteration_check
07c17d67 772 : public traversing_visitor
67c0a579
GH
773{
774 systemtap_session & session;
d02548c0
GH
775 map<functiondecl *,set<vardecl *> *> & function_mutates_vars;
776 vector<vardecl *> vars_being_iterated;
57148ee7
FCE
777
778 no_var_mutation_during_iteration_check
67c0a579 779 (systemtap_session & sess,
d02548c0
GH
780 map<functiondecl *,set<vardecl *> *> & fmv)
781 : session(sess), function_mutates_vars (fmv)
67c0a579
GH
782 {}
783
784 void visit_arrayindex (arrayindex *e)
785 {
d7f3e0c5 786 if (is_active_lvalue(e))
67c0a579 787 {
d02548c0
GH
788 vardecl *vd = get_symbol_within_indexable (e->base)->referent;
789 if (vd)
67c0a579 790 {
d02548c0 791 for (unsigned i = 0; i < vars_being_iterated.size(); ++i)
67c0a579 792 {
d02548c0
GH
793 vardecl *v = vars_being_iterated[i];
794 if (v == vd)
795 {
796 string err = ("variable '" + v->name +
797 "' modified during 'foreach' iteration");
798 session.print_error (semantic_error (err, e->tok));
799 }
67c0a579
GH
800 }
801 }
802 }
1bbeef03 803 traversing_visitor::visit_arrayindex (e);
67c0a579
GH
804 }
805
806 void visit_functioncall (functioncall* e)
807 {
57148ee7 808 map<functiondecl *,set<vardecl *> *>::const_iterator i
d02548c0 809 = function_mutates_vars.find (e->referent);
67c0a579 810
d02548c0 811 if (i != function_mutates_vars.end())
67c0a579 812 {
d02548c0 813 for (unsigned j = 0; j < vars_being_iterated.size(); ++j)
67c0a579 814 {
d02548c0 815 vardecl *m = vars_being_iterated[j];
67c0a579
GH
816 if (i->second->find (m) != i->second->end())
817 {
d02548c0 818 string err = ("function call modifies var '" + m->name +
67c0a579
GH
819 "' during 'foreach' iteration");
820 session.print_error (semantic_error (err, e->tok));
821 }
822 }
823 }
824
1bbeef03 825 traversing_visitor::visit_functioncall (e);
67c0a579
GH
826 }
827
828 void visit_foreach_loop(foreach_loop* s)
829 {
d02548c0
GH
830 vardecl *vd = get_symbol_within_indexable (s->base)->referent;
831
832 if (vd)
833 vars_being_iterated.push_back (vd);
57148ee7 834
1bbeef03 835 traversing_visitor::visit_foreach_loop (s);
d02548c0
GH
836
837 if (vd)
838 vars_being_iterated.pop_back();
67c0a579
GH
839 }
840};
20c6c071 841
2b066ec1 842
67c0a579
GH
843// ------------------------------------------------------------------------
844
07c17d67
GH
845struct stat_decl_collector
846 : public traversing_visitor
847{
848 systemtap_session & session;
57148ee7 849
07c17d67
GH
850 stat_decl_collector(systemtap_session & sess)
851 : session(sess)
852 {}
853
854 void visit_stat_op (stat_op* e)
855 {
856 symbol *sym = get_symbol_within_expression (e->stat);
857 if (session.stat_decls.find(sym->name) == session.stat_decls.end())
858 session.stat_decls[sym->name] = statistic_decl();
859 }
860
861 void visit_assignment (assignment* e)
862 {
863 if (e->op == "<<<")
864 {
865 symbol *sym = get_symbol_within_expression (e->left);
866 if (session.stat_decls.find(sym->name) == session.stat_decls.end())
867 session.stat_decls[sym->name] = statistic_decl();
868 }
869 else
870 traversing_visitor::visit_assignment(e);
871 }
872
873 void visit_hist_op (hist_op* e)
874 {
875 symbol *sym = get_symbol_within_expression (e->stat);
876 statistic_decl new_stat;
877
878 if (e->htype == hist_linear)
879 {
880 new_stat.type = statistic_decl::linear;
881 assert (e->params.size() == 3);
882 new_stat.linear_low = e->params[0];
883 new_stat.linear_high = e->params[1];
884 new_stat.linear_step = e->params[2];
885 }
886 else
887 {
888 assert (e->htype == hist_log);
889 new_stat.type = statistic_decl::logarithmic;
e38723d2 890 assert (e->params.size() == 0);
07c17d67
GH
891 }
892
893 map<string, statistic_decl>::iterator i = session.stat_decls.find(sym->name);
894 if (i == session.stat_decls.end())
895 session.stat_decls[sym->name] = new_stat;
896 else
897 {
898 statistic_decl & old_stat = i->second;
899 if (!(old_stat == new_stat))
900 {
901 if (old_stat.type == statistic_decl::none)
902 i->second = new_stat;
903 else
904 {
905 // FIXME: Support multiple co-declared histogram types
57148ee7 906 semantic_error se("multiple histogram types declared on '" + sym->name + "'",
07c17d67
GH
907 e->tok);
908 session.print_error (se);
909 }
910 }
57148ee7 911 }
07c17d67
GH
912 }
913
914};
915
916static int
917semantic_pass_stats (systemtap_session & sess)
918{
919 stat_decl_collector sdc(sess);
920
f76427a2
FCE
921 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
922 it->second->body->visit (&sdc);
07c17d67 923
57148ee7 924 for (unsigned i = 0; i < sess.probes.size(); ++i)
07c17d67
GH
925 sess.probes[i]->body->visit (&sdc);
926
927 for (unsigned i = 0; i < sess.globals.size(); ++i)
928 {
929 vardecl *v = sess.globals[i];
930 if (v->type == pe_stats)
931 {
57148ee7 932
07c17d67
GH
933 if (sess.stat_decls.find(v->name) == sess.stat_decls.end())
934 {
935 semantic_error se("unable to infer statistic parameters for global '" + v->name + "'");
936 sess.print_error (se);
937 }
938 }
939 }
57148ee7 940
7e41d3dc 941 return sess.num_errors();
07c17d67
GH
942}
943
5d23847d
FCE
944// ------------------------------------------------------------------------
945
946// Enforce variable-related invariants: no modification of
947// a foreach()-iterated array.
948static int
949semantic_pass_vars (systemtap_session & sess)
950{
57148ee7 951
5d23847d
FCE
952 map<functiondecl *, set<vardecl *> *> fmv;
953 no_var_mutation_during_iteration_check chk(sess, fmv);
57148ee7 954
f76427a2 955 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
5d23847d 956 {
f76427a2 957 functiondecl * fn = it->second;
5d23847d
FCE
958 if (fn->body)
959 {
960 set<vardecl *> * m = new set<vardecl *>();
961 mutated_var_collector mc (m);
962 fn->body->visit (&mc);
963 fmv[fn] = m;
964 }
965 }
966
f76427a2 967 for (map<string,functiondecl*>::iterator it = sess.functions.begin(); it != sess.functions.end(); it++)
5d23847d 968 {
f76427a2
FCE
969 functiondecl * fn = it->second;
970 if (fn->body) fn->body->visit (&chk);
5d23847d
FCE
971 }
972
973 for (unsigned i = 0; i < sess.probes.size(); ++i)
974 {
975 if (sess.probes[i]->body)
976 sess.probes[i]->body->visit (&chk);
57148ee7 977 }
5d23847d
FCE
978
979 return sess.num_errors();
980}
981
982
983// ------------------------------------------------------------------------
984
985// Rewrite probe condition expressions into probe bodies. Tricky and
986// exciting business, this. This:
987//
988// probe foo if (g1 || g2) { ... }
989// probe bar { ... g1 ++ ... }
990//
991// becomes:
992//
993// probe begin(MAX) { if (! (g1 || g2)) %{ disable_probe_foo %} }
994// probe foo { if (! (g1 || g2)) next; ... }
57148ee7 995// probe bar { ... g1 ++ ...;
5d23847d
FCE
996// if (g1 || g2) %{ enable_probe_foo %} else %{ disable_probe_foo %}
997// }
998//
999// XXX: As a first cut, do only the "inline probe condition" part of the
1000// transform.
1001
1002static int
1003semantic_pass_conditions (systemtap_session & sess)
1004{
1005 for (unsigned i = 0; i < sess.probes.size(); ++i)
1006 {
1007 derived_probe* p = sess.probes[i];
1008 expression* e = p->sole_location()->condition;
1009 if (e)
1010 {
1011 varuse_collecting_visitor vut;
1012 e->visit (& vut);
1013
1014 if (! vut.written.empty())
1015 {
1016 string err = ("probe condition must not modify any variables");
1017 sess.print_error (semantic_error (err, e->tok));
1018 }
1019 else if (vut.embedded_seen)
1020 {
1021 sess.print_error (semantic_error ("probe condition must not include impure embedded-C", e->tok));
1022 }
1023
1024 // Add the condition expression to the front of the
1025 // derived_probe body.
1026 if_statement *ifs = new if_statement ();
1027 ifs->tok = e->tok;
1028 ifs->thenblock = new next_statement ();
1029 ifs->thenblock->tok = e->tok;
1030 ifs->elseblock = NULL;
1031 unary_expression *notex = new unary_expression ();
1032 notex->op = "!";
1033 notex->tok = e->tok;
1034 notex->operand = e;
1035 ifs->condition = notex;
ba6f838d 1036 p->body = new block (ifs, p->body);
5d23847d 1037 }
57148ee7 1038 }
5d23847d
FCE
1039
1040 return sess.num_errors();
1041}
1042
1043
07c17d67
GH
1044// ------------------------------------------------------------------------
1045
2b066ec1
FCE
1046
1047static int semantic_pass_symbols (systemtap_session&);
c214bd6a
DS
1048static int semantic_pass_optimize1 (systemtap_session&);
1049static int semantic_pass_optimize2 (systemtap_session&);
2b066ec1 1050static int semantic_pass_types (systemtap_session&);
d02548c0 1051static int semantic_pass_vars (systemtap_session&);
07c17d67 1052static int semantic_pass_stats (systemtap_session&);
5d23847d 1053static int semantic_pass_conditions (systemtap_session&);
2b066ec1
FCE
1054
1055
1056// Link up symbols to their declarations. Set the session's
1057// files/probes/functions/globals vectors from the transitively
1058// reached set of stapfiles in s.library_files, starting from
20c6c071 1059// s.user_file. Perform automatic tapset inclusion and probe
2b066ec1
FCE
1060// alias expansion.
1061static int
1062semantic_pass_symbols (systemtap_session& s)
1063{
1064 symresolution_info sym (s);
1065
1066 // NB: s.files can grow during this iteration, so size() can
1067 // return gradually increasing numbers.
1068 s.files.push_back (s.user_file);
1069 for (unsigned i = 0; i < s.files.size(); i++)
1070 {
49abf162 1071 if (pending_interrupts) break;
2b066ec1
FCE
1072 stapfile* dome = s.files[i];
1073
1074 // Pass 1: add globals and functions to systemtap-session master list,
1075 // so the find_* functions find them
1076
1077 for (unsigned i=0; i<dome->globals.size(); i++)
1078 s.globals.push_back (dome->globals[i]);
1079
1080 for (unsigned i=0; i<dome->functions.size(); i++)
f76427a2 1081 s.functions[dome->functions[i]->name] = dome->functions[i];
2b066ec1 1082
54dfabe9
FCE
1083 for (unsigned i=0; i<dome->embeds.size(); i++)
1084 s.embeds.push_back (dome->embeds[i]);
1085
2b066ec1
FCE
1086 // Pass 2: process functions
1087
1088 for (unsigned i=0; i<dome->functions.size(); i++)
1089 {
49abf162 1090 if (pending_interrupts) break;
2b066ec1
FCE
1091 functiondecl* fd = dome->functions[i];
1092
57148ee7 1093 try
2b066ec1 1094 {
f80d9004
JS
1095 for (unsigned j=0; j<s.code_filters.size(); j++)
1096 fd->body = s.code_filters[j]->require (fd->body);
1097
8a43522c
GH
1098 sym.current_function = fd;
1099 sym.current_probe = 0;
1100 fd->body->visit (& sym);
2b066ec1
FCE
1101 }
1102 catch (const semantic_error& e)
1103 {
1104 s.print_error (e);
1105 }
1106 }
1107
5227f1ea 1108 // Pass 3: derive probes and resolve any further symbols in the
57148ee7 1109 // derived results.
2b066ec1
FCE
1110
1111 for (unsigned i=0; i<dome->probes.size(); i++)
1112 {
49abf162 1113 if (pending_interrupts) break;
2b066ec1
FCE
1114 probe* p = dome->probes [i];
1115 vector<derived_probe*> dps;
1116
a971b891
FCE
1117 // much magic happens here: probe alias expansion, wildcard
1118 // matching, low-level derived_probe construction.
b4ceace2 1119 derive_probes (s, p, dps);
2b066ec1
FCE
1120
1121 for (unsigned j=0; j<dps.size(); j++)
1122 {
49abf162 1123 if (pending_interrupts) break;
2b066ec1 1124 derived_probe* dp = dps[j];
b20febf3
FCE
1125 s.probes.push_back (dp);
1126 dp->join_group (s);
2b066ec1 1127
57148ee7 1128 try
2b066ec1 1129 {
f80d9004
JS
1130 for (unsigned k=0; k<s.code_filters.size(); k++)
1131 dp->body = s.code_filters[k]->require (dp->body);
1132
2b066ec1 1133 sym.current_function = 0;
5227f1ea 1134 sym.current_probe = dp;
2b066ec1 1135 dp->body->visit (& sym);
5d23847d
FCE
1136
1137 // Process the probe-point condition expression.
1138 sym.current_function = 0;
1139 sym.current_probe = 0;
1140 if (dp->sole_location()->condition)
1141 dp->sole_location()->condition->visit (& sym);
2b066ec1
FCE
1142 }
1143 catch (const semantic_error& e)
1144 {
1145 s.print_error (e);
1146 }
1147 }
1148 }
1149 }
aa30ccd3
FCE
1150
1151 // Inform all derived_probe builders that we're done with
1152 // all resolution, so it's time to release caches.
1153 s.pattern_root->build_no_more (s);
57148ee7 1154
7e41d3dc 1155 return s.num_errors(); // all those print_error calls
2b066ec1
FCE
1156}
1157
1158
0a102c82
SC
1159// Keep unread global variables for probe end value display.
1160void add_global_var_display (systemtap_session& s)
1161{
5ae31731
FCE
1162 // Don't generate synthetic end probes when in listings mode;
1163 // it would clutter up the list of probe points with "end ...".
1164 if (s.listing_mode) return;
3438f38f 1165
0a102c82
SC
1166 varuse_collecting_visitor vut;
1167 for (unsigned i=0; i<s.probes.size(); i++)
1168 {
1169 s.probes[i]->body->visit (& vut);
1170
1171 if (s.probes[i]->sole_location()->condition)
1172 s.probes[i]->sole_location()->condition->visit (& vut);
1173 }
1174
1175 for (unsigned g=0; g < s.globals.size(); g++)
1176 {
1177 vardecl* l = s.globals[g];
1178 if (vut.read.find (l) != vut.read.end()
e491a713 1179 || vut.written.find (l) == vut.written.end())
0a102c82
SC
1180 continue;
1181
b5852334 1182 // Don't generate synthetic end probes for unread globals
5ae31731
FCE
1183 // declared only within tapsets. (RHBZ 468139), but rather
1184 // only within the end-user script.
1185
7584f162
RA
1186 bool tapset_global = false;
1187 for (size_t m=0; m < s.library_files.size(); m++)
1188 {
1189 for (size_t n=0; n < s.library_files[m]->globals.size(); n++)
1190 {
1191 if (l->name == s.library_files[m]->globals[n]->name)
1192 {tapset_global = true; break;}
1193 }
1194 }
1195 if (tapset_global)
1196 continue;
5ae31731 1197
0a102c82
SC
1198 print_format* pf = new print_format;
1199 probe* p = new probe;
1200 probe_point* pl = new probe_point;
1201 probe_point::component* c = new probe_point::component("end");
1202 token* print_tok = new token;
1203 vector<derived_probe*> dps;
6d2685fe 1204 block *b = new block;
0a102c82
SC
1205
1206 pl->components.push_back (c);
db48cbe9 1207 p->tok = l->tok;
0a102c82
SC
1208 p->locations.push_back (pl);
1209 print_tok->type = tok_identifier;
1210 print_tok->content = "printf";
1211
1212 // Create a symbol
1213 symbol* g_sym = new symbol;
1214 g_sym->name = l->name;
1215 g_sym->tok = l->tok;
1216 g_sym->type = l->type;
57148ee7 1217 g_sym->referent = l;
0a102c82
SC
1218
1219 pf->print_to_stream = true;
1220 pf->print_with_format = true;
1221 pf->print_with_delim = false;
1222 pf->print_with_newline = false;
1223 pf->print_char = false;
1224 pf->raw_components += l->name;
1225 pf->tok = print_tok;
1226
1227 if (l->index_types.size() == 0) // Scalar
1228 {
e071e49b
SC
1229 if (l->type == pe_stats)
1230 pf->raw_components += " @count=%#x @min=%#x @max=%#x @sum=%#x @avg=%#x\\n";
1231 else if (l->type == pe_string)
0a102c82
SC
1232 pf->raw_components += "=\"%#s\"\\n";
1233 else
1234 pf->raw_components += "=%#x\\n";
0a102c82
SC
1235 pf->components = print_format::string_to_components(pf->raw_components);
1236 expr_statement* feb = new expr_statement;
1237 feb->value = pf;
1238 feb->tok = print_tok;
e071e49b
SC
1239 if (l->type == pe_stats)
1240 {
1241 struct stat_op* so [5];
1242 const stat_component_type stypes[] = {sc_count, sc_min, sc_max, sc_sum, sc_average};
1243
1244 for (unsigned si = 0;
1245 si < (sizeof(so)/sizeof(struct stat_op*));
1246 si++)
1247 {
1248 so[si]= new stat_op;
1249 so[si]->ctype = stypes[si];
1250 so[si]->type = pe_long;
1251 so[si]->stat = g_sym;
1252 so[si]->tok = l->tok;
1253 pf->args.push_back(so[si]);
1254 }
1255 }
1256 else
1257 pf->args.push_back(g_sym);
271d408e
WH
1258
1259 /* PR7053: Checking empty aggregate for global variable */
1260 if (l->type == pe_stats) {
1261 stat_op *so= new stat_op;
1262 so->ctype = sc_count;
1263 so->type = pe_long;
1264 so->stat = g_sym;
1265 so->tok = l->tok;
1266 comparison *be = new comparison;
1267 be->op = ">";
1268 be->tok = l->tok;
1269 be->left = so;
1270 be->right = new literal_number(0);
1271
1272 /* Create printf @count=0x0 in else block */
1273 print_format* pf_0 = new print_format;
1274 pf_0->print_to_stream = true;
1275 pf_0->print_with_format = true;
1276 pf_0->print_with_delim = false;
1277 pf_0->print_with_newline = false;
1278 pf_0->print_char = false;
1279 pf_0->raw_components += l->name;
1280 pf_0->raw_components += " @count=0x0\\n";
1281 pf_0->tok = print_tok;
1282 pf_0->components = print_format::string_to_components(pf_0->raw_components);
1283 expr_statement* feb_else = new expr_statement;
1284 feb_else->value = pf_0;
1285 feb_else->tok = print_tok;
1286 if_statement *ifs = new if_statement;
1287 ifs->tok = l->tok;
1288 ifs->condition = be;
1289 ifs->thenblock = feb ;
1290 ifs->elseblock = feb_else;
1291 b->statements.push_back(ifs);
1292 }
1293 else /* other non-stat cases */
1294 b->statements.push_back(feb);
0a102c82
SC
1295 }
1296 else // Array
1297 {
1298 int idx_count = l->index_types.size();
0a102c82
SC
1299 symbol* idx_sym[idx_count];
1300 vardecl* idx_v[idx_count];
1301 // Create a foreach loop
0a102c82 1302 foreach_loop* fe = new foreach_loop;
e91b23bc
FCE
1303 fe->sort_direction = -1; // imply decreasing sort on value
1304 fe->sort_column = 0; // as in foreach ([a,b,c] in array-) { }
0a102c82
SC
1305 fe->limit = NULL;
1306
1307 // Create indices for the foreach loop
1308 for (int i=0; i < idx_count; i++)
1309 {
0a102c82
SC
1310 char *idx_name;
1311 if (asprintf (&idx_name, "idx%d", i) < 0)
1312 return;
0a102c82 1313 idx_sym[i] = new symbol;
0a102c82 1314 idx_sym[i]->name = idx_name;
db48cbe9 1315 idx_sym[i]->tok = l->tok;
0a102c82
SC
1316 idx_v[i] = new vardecl;
1317 idx_v[i]->name = idx_name;
0a102c82 1318 idx_v[i]->type = l->index_types[i];
db48cbe9 1319 idx_v[i]->tok = l->tok;
0a102c82
SC
1320 idx_sym[i]->referent = idx_v[i];
1321 fe->indexes.push_back (idx_sym[i]);
1322 }
1323
1324 // Create a printf for the foreach loop
57148ee7 1325 pf->raw_components += "[";
0a102c82 1326 for (int i=0; i < idx_count; i++)
57148ee7
FCE
1327 {
1328 if (i > 0)
1329 pf->raw_components += ",";
1330 if (l->index_types[i] == pe_string)
1331 pf->raw_components += "\"%#s\"";
1332 else
1333 pf->raw_components += "%#d";
1334 }
1335 pf->raw_components += "]";
e491a713
SC
1336 if (l->type == pe_stats)
1337 pf->raw_components += " @count=%#x @min=%#x @max=%#x @sum=%#x @avg=%#x\\n";
1338 else if (l->type == pe_string)
0a102c82
SC
1339 pf->raw_components += "=\"%#s\"\\n";
1340 else
1341 pf->raw_components += "=%#x\\n";
1342
1343 // Create an index for the array
1344 struct arrayindex* ai = new arrayindex;
1345 ai->tok = l->tok;
1346 ai->base = g_sym;
e491a713 1347
0a102c82
SC
1348 for (int i=0; i < idx_count; i++)
1349 {
1350 ai->indexes.push_back (idx_sym[i]);
1351 pf->args.push_back(idx_sym[i]);
1352 }
e491a713
SC
1353 if (l->type == pe_stats)
1354 {
1355 struct stat_op* so [5];
e491a713 1356 const stat_component_type stypes[] = {sc_count, sc_min, sc_max, sc_sum, sc_average};
e071e49b
SC
1357
1358 ai->type = pe_stats;
db48cbe9
SC
1359 for (unsigned si = 0;
1360 si < (sizeof(so)/sizeof(struct stat_op*));
1361 si++)
e491a713
SC
1362 {
1363 so[si]= new stat_op;
1364 so[si]->ctype = stypes[si];
1365 so[si]->type = pe_long;
1366 so[si]->stat = ai;
db48cbe9 1367 so[si]->tok = l->tok;
e071e49b 1368 pf->args.push_back(so[si]);
e491a713 1369 }
e491a713
SC
1370 }
1371 else
1372 pf->args.push_back(ai);
57148ee7 1373
0a102c82
SC
1374 pf->components = print_format::string_to_components(pf->raw_components);
1375 expr_statement* feb = new expr_statement;
1376 feb->value = pf;
0a102c82
SC
1377 fe->base = g_sym;
1378 fe->block = (statement*)feb;
1379 b->statements.push_back(fe);
0a102c82
SC
1380 }
1381
0a102c82 1382 // Add created probe
6d2685fe
SC
1383 p->body = b;
1384 derive_probes (s, p, dps);
0a102c82
SC
1385 for (unsigned i = 0; i < dps.size(); i++)
1386 {
1387 derived_probe* dp = dps[i];
1388 s.probes.push_back (dp);
1389 dp->join_group (s);
1390 }
6d2685fe
SC
1391 // Repopulate symbol and type info
1392 symresolution_info sym (s);
1393 sym.current_function = 0;
1394 sym.current_probe = dps[0];
1395 dps[0]->body->visit (& sym);
0a102c82 1396
6d2685fe
SC
1397 semantic_pass_types(s);
1398 // Mark that variable is read
0a102c82
SC
1399 vut.read.insert (l);
1400 }
1401}
2b066ec1
FCE
1402
1403int
1404semantic_pass (systemtap_session& s)
1405{
59bafbe8 1406 int rc = 0;
20c6c071 1407
57148ee7 1408 try
59bafbe8
FCE
1409 {
1410 s.register_library_aliases();
1411 register_standard_tapsets(s);
57148ee7 1412
5d23847d
FCE
1413 if (rc == 0) rc = semantic_pass_symbols (s);
1414 if (rc == 0) rc = semantic_pass_conditions (s);
c214bd6a 1415 if (rc == 0 && ! s.unoptimized) rc = semantic_pass_optimize1 (s);
59bafbe8 1416 if (rc == 0) rc = semantic_pass_types (s);
0a102c82 1417 if (rc == 0) add_global_var_display (s);
c214bd6a 1418 if (rc == 0 && ! s.unoptimized) rc = semantic_pass_optimize2 (s);
d02548c0 1419 if (rc == 0) rc = semantic_pass_vars (s);
07c17d67 1420 if (rc == 0) rc = semantic_pass_stats (s);
57148ee7 1421
277f2b79
FCE
1422 if (s.probes.size() == 0 && !s.listing_mode)
1423 throw semantic_error ("no probes found");
59bafbe8
FCE
1424 }
1425 catch (const semantic_error& e)
1426 {
1427 s.print_error (e);
21beacc9 1428 rc ++;
59bafbe8 1429 }
57148ee7 1430
2b066ec1
FCE
1431 return rc;
1432}
1433
1434
1435// ------------------------------------------------------------------------
1436
1437
1438systemtap_session::systemtap_session ():
b20febf3 1439 // NB: pointer members must be manually initialized!
20c6c071 1440 pattern_root(new match_node),
b20febf3 1441 user_file (0),
57148ee7
FCE
1442 be_derived_probes(0),
1443 dwarf_derived_probes(0),
1444 uprobe_derived_probes(0),
1445 utrace_derived_probes(0),
1446 itrace_derived_probes(0),
1447 task_finder_derived_probes(0),
1448 timer_derived_probes(0),
1449 profile_derived_probes(0),
1450 mark_derived_probes(0),
1451 hrtimer_derived_probes(0),
1452 perfmon_derived_probes(0),
1453 procfs_derived_probes(0),
6561773f
FCE
1454 op (0), up (0),
1455 sym_kprobes_text_start (0),
1456 sym_kprobes_text_end (0),
cfd621bc 1457 sym_stext (0),
405b71b8 1458 module_cache (0),
cfd621bc 1459 last_token (0)
2b066ec1
FCE
1460{
1461}
1462
1463
cfd621bc
FCE
1464// Print this given token, but abbreviate it if the last one had the
1465// same file name.
1466void
1467systemtap_session::print_token (ostream& o, const token* tok)
1468{
1469 assert (tok);
1470
1471 if (last_token && last_token->location.file == tok->location.file)
1472 {
1473 stringstream tmpo;
1474 tmpo << *tok;
1475 string ts = tmpo.str();
1476 // search & replace the file name with nothing
1477 size_t idx = ts.find (tok->location.file);
1478 if (idx != string::npos)
1479 ts.replace (idx, tok->location.file.size(), "");
57148ee7 1480
cfd621bc
FCE
1481 o << ts;
1482 }
1483 else
1484 o << *tok;
1485
1486 last_token = tok;
1487}
1488
1489
1490
2b066ec1
FCE
1491void
1492systemtap_session::print_error (const semantic_error& e)
1493{
b487a14d 1494 string message_str[2];
1b1b4ceb 1495 string align_semantic_error (" ");
7e41d3dc 1496
b487a14d
FCE
1497 // We generate two messages. The second one ([1]) is printed
1498 // without token compression, for purposes of duplicate elimination.
1499 // This way, the same message that may be generated once with a
1500 // compressed and once with an uncompressed token still only gets
1501 // printed once.
1502 for (int i=0; i<2; i++)
1503 {
1504 stringstream message;
fecccf83 1505
b487a14d
FCE
1506 message << "semantic error: " << e.what ();
1507 if (e.tok1 || e.tok2)
1508 message << ": ";
fecccf83 1509 if (e.tok1)
b487a14d
FCE
1510 {
1511 if (i == 0) print_token (message, e.tok1);
1512 else message << *e.tok1;
1513 }
1514 message << e.msg2;
fecccf83 1515 if (e.tok2)
b487a14d
FCE
1516 {
1517 if (i == 0) print_token (message, e.tok2);
1518 else message << *e.tok2;
1519 }
1520 message << endl;
1521 message_str[i] = message.str();
1522 }
7e41d3dc
FCE
1523
1524 // Duplicate elimination
b487a14d 1525 if (seen_errors.find (message_str[1]) == seen_errors.end())
7e41d3dc 1526 {
b487a14d
FCE
1527 seen_errors.insert (message_str[1]);
1528 cerr << message_str[0];
1b1b4ceb
RA
1529
1530 if (e.tok1)
1531 print_error_source (cerr, align_semantic_error, e.tok1);
1532
1533 if (e.tok2)
1534 print_error_source (cerr, align_semantic_error, e.tok2);
7e41d3dc
FCE
1535 }
1536
1537 if (e.chain)
1538 print_error (* e.chain);
2b066ec1
FCE
1539}
1540
1b1b4ceb
RA
1541void
1542systemtap_session::print_error_source (std::ostream& message,
1543 std::string& align, const token* tok)
1544{
1545 unsigned i = 0;
1546 unsigned line = tok->location.line;
1547 unsigned col = tok->location.column;
1548 string file_contents;
84680f7e
FCE
1549
1550 assert (tok);
1b1b4ceb
RA
1551 if (tok->location.stap_file)
1552 file_contents = tok->location.stap_file->file_contents;
1553 else
1554 //No source to print, silently exit
1555 return;
1556 size_t start_pos = 0, end_pos = 0;
1557 //Navigate to the appropriate line
1558 while (i != line && end_pos != std::string::npos)
1559 {
1560 start_pos = end_pos;
1561 end_pos = file_contents.find ('\n', start_pos) + 1;
1562 i++;
1563 }
1564 message << align << "source: " << file_contents.substr (start_pos, end_pos-start_pos-1) << endl;
1565 message << align << " ";
1566 //Navigate to the appropriate column
1567 for (i=start_pos; i<start_pos+col-1; i++)
1568 {
1569 if(isspace(file_contents[i]))
1570 message << file_contents[i];
1571 else
1572 message << ' ';
1573 }
1574 message << "^" << endl;
1575}
1576
ab54fa85 1577void
cfd621bc 1578systemtap_session::print_warning (const string& message_str, const token* tok)
ab54fa85 1579{
ab54fa85 1580 // Duplicate elimination
1b1b4ceb 1581 string align_warning (" ");
f2782fe5 1582 if (seen_warnings.find (message_str) == seen_warnings.end())
ab54fa85 1583 {
ee448707 1584 seen_warnings.insert (message_str);
cfd621bc
FCE
1585 clog << "WARNING: " << message_str;
1586 if (tok) { clog << ": "; print_token (clog, tok); }
1587 clog << endl;
84680f7e 1588 if (tok) { print_error_source (clog, align_warning, tok); }
ab54fa85
SC
1589 }
1590}
1591
2b066ec1
FCE
1592
1593// ------------------------------------------------------------------------
1594// semantic processing: symbol resolution
1595
1596
1597symresolution_info::symresolution_info (systemtap_session& s):
5227f1ea 1598 session (s), current_function (0), current_probe (0)
2b066ec1
FCE
1599{
1600}
1601
1602
1603void
1604symresolution_info::visit_block (block* e)
1605{
1606 for (unsigned i=0; i<e->statements.size(); i++)
1607 {
57148ee7 1608 try
2b066ec1
FCE
1609 {
1610 e->statements[i]->visit (this);
1611 }
1612 catch (const semantic_error& e)
1613 {
1614 session.print_error (e);
1615 }
1616 }
1617}
1618
1619
69c68955
FCE
1620void
1621symresolution_info::visit_foreach_loop (foreach_loop* e)
1622{
1623 for (unsigned i=0; i<e->indexes.size(); i++)
1624 e->indexes[i]->visit (this);
1625
57148ee7 1626 symbol *array = NULL;
d02548c0
GH
1627 hist_op *hist = NULL;
1628 classify_indexable (e->base, array, hist);
69c68955 1629
d02548c0
GH
1630 if (array)
1631 {
1632 if (!array->referent)
57148ee7 1633 {
d02548c0
GH
1634 vardecl* d = find_var (array->name, e->indexes.size ());
1635 if (d)
1636 array->referent = d;
1637 else
2a99f48f 1638 {
7e41d3dc
FCE
1639 stringstream msg;
1640 msg << "unresolved arity-" << e->indexes.size()
1641 << " global array " << array->name;
1642 throw semantic_error (msg.str(), e->tok);
2a99f48f 1643 }
d02548c0
GH
1644 }
1645 }
57148ee7 1646 else
d02548c0
GH
1647 {
1648 assert (hist);
1649 hist->visit (this);
1650 }
69c68955 1651
27f21e8c
DS
1652 if (e->limit)
1653 e->limit->visit (this);
1654
69c68955
FCE
1655 e->block->visit (this);
1656}
1657
d02548c0 1658
57148ee7 1659struct
d98d459c
GH
1660delete_statement_symresolution_info:
1661 public traversing_visitor
1662{
1663 symresolution_info *parent;
1664
1665 delete_statement_symresolution_info (symresolution_info *p):
1666 parent(p)
1667 {}
1668
1669 void visit_arrayindex (arrayindex* e)
1670 {
1671 parent->visit_arrayindex (e);
1672 }
1673 void visit_functioncall (functioncall* e)
1674 {
1675 parent->visit_functioncall (e);
1676 }
1677
1678 void visit_symbol (symbol* e)
1679 {
1680 if (e->referent)
1681 return;
57148ee7 1682
d98d459c
GH
1683 vardecl* d = parent->find_var (e->name, -1);
1684 if (d)
1685 e->referent = d;
1686 else
1687 throw semantic_error ("unresolved array in delete statement", e->tok);
1688 }
1689};
1690
57148ee7 1691void
d98d459c
GH
1692symresolution_info::visit_delete_statement (delete_statement* s)
1693{
1694 delete_statement_symresolution_info di (this);
1695 s->value->visit (&di);
1696}
1697
69c68955 1698
2b066ec1
FCE
1699void
1700symresolution_info::visit_symbol (symbol* e)
1701{
1702 if (e->referent)
1703 return;
1704
313b2f74 1705 vardecl* d = find_var (e->name, 0);
2b066ec1
FCE
1706 if (d)
1707 e->referent = d;
1708 else
1709 {
1710 // new local
1711 vardecl* v = new vardecl;
1712 v->name = e->name;
1713 v->tok = e->tok;
1714 if (current_function)
1715 current_function->locals.push_back (v);
1716 else if (current_probe)
1717 current_probe->locals.push_back (v);
1718 else
5d23847d
FCE
1719 // must be probe-condition expression
1720 throw semantic_error ("probe condition must not reference undeclared global", e->tok);
2b066ec1
FCE
1721 e->referent = v;
1722 }
1723}
1724
1725
1726void
1727symresolution_info::visit_arrayindex (arrayindex* e)
1728{
1729 for (unsigned i=0; i<e->indexes.size(); i++)
1730 e->indexes[i]->visit (this);
1731
57148ee7 1732 symbol *array = NULL;
d02548c0
GH
1733 hist_op *hist = NULL;
1734 classify_indexable(e->base, array, hist);
2b066ec1 1735
d02548c0 1736 if (array)
313b2f74 1737 {
d02548c0
GH
1738 if (array->referent)
1739 return;
1740
1741 vardecl* d = find_var (array->name, e->indexes.size ());
1742 if (d)
1743 array->referent = d;
313b2f74 1744 else
d02548c0
GH
1745 {
1746 // new local
1747 vardecl* v = new vardecl;
1748 v->set_arity(e->indexes.size());
1749 v->name = array->name;
1750 v->tok = array->tok;
1751 if (current_function)
1752 current_function->locals.push_back (v);
1753 else if (current_probe)
1754 current_probe->locals.push_back (v);
1755 else
1756 // must not happen
1757 throw semantic_error ("no current probe/function", e->tok);
1758 array->referent = v;
57148ee7 1759 }
d02548c0
GH
1760 }
1761 else
1762 {
1763 assert (hist);
1764 hist->visit (this);
313b2f74 1765 }
2b066ec1
FCE
1766}
1767
1768
1769void
1770symresolution_info::visit_functioncall (functioncall* e)
1771{
5d23847d
FCE
1772 // XXX: we could relax this, if we're going to examine the
1773 // vartracking data recursively. See testsuite/semko/fortytwo.stp.
1774 if (! (current_function || current_probe))
1775 {
1776 // must be probe-condition expression
1777 throw semantic_error ("probe condition must not reference function", e->tok);
1778 }
1779
2b066ec1
FCE
1780 for (unsigned i=0; i<e->args.size(); i++)
1781 e->args[i]->visit (this);
1782
1783 if (e->referent)
1784 return;
1785
1786 functiondecl* d = find_function (e->function, e->args.size ());
1787 if (d)
1788 e->referent = d;
1789 else
2a99f48f 1790 {
7e41d3dc
FCE
1791 stringstream msg;
1792 msg << "unresolved arity-" << e->args.size()
1793 << " function";
1794 throw semantic_error (msg.str(), e->tok);
2a99f48f 1795 }
2b066ec1
FCE
1796}
1797
1798
57148ee7 1799vardecl*
d98d459c 1800symresolution_info::find_var (const string& name, int arity)
2b066ec1 1801{
5d23847d
FCE
1802 if (current_function || current_probe)
1803 {
1804 // search locals
57148ee7 1805 vector<vardecl*>& locals = (current_function ?
5d23847d
FCE
1806 current_function->locals :
1807 current_probe->locals);
57148ee7
FCE
1808
1809
5d23847d 1810 for (unsigned i=0; i<locals.size(); i++)
57148ee7 1811 if (locals[i]->name == name
5d23847d
FCE
1812 && locals[i]->compatible_arity(arity))
1813 {
1814 locals[i]->set_arity (arity);
1815 return locals[i];
1816 }
1817 }
2b066ec1 1818
313b2f74
GH
1819 // search function formal parameters (for scalars)
1820 if (arity == 0 && current_function)
2b066ec1
FCE
1821 for (unsigned i=0; i<current_function->formal_args.size(); i++)
1822 if (current_function->formal_args[i]->name == name)
8846477c
FCE
1823 {
1824 // NB: no need to check arity here: formal args always scalar
1825 current_function->formal_args[i]->set_arity (0);
1826 return current_function->formal_args[i];
1827 }
2b066ec1 1828
313b2f74 1829 // search processed globals
2b066ec1 1830 for (unsigned i=0; i<session.globals.size(); i++)
313b2f74 1831 if (session.globals[i]->name == name
57148ee7 1832 && session.globals[i]->compatible_arity(arity))
8846477c 1833 {
313b2f74 1834 session.globals[i]->set_arity (arity);
8846477c
FCE
1835 return session.globals[i];
1836 }
57148ee7 1837
2b066ec1
FCE
1838 // search library globals
1839 for (unsigned i=0; i<session.library_files.size(); i++)
1840 {
1841 stapfile* f = session.library_files[i];
1842 for (unsigned j=0; j<f->globals.size(); j++)
84e5ea0f
FCE
1843 {
1844 vardecl* g = f->globals[j];
1845 if (g->name == name && g->compatible_arity (arity))
1846 {
d98d459c 1847 g->set_arity (arity);
57148ee7
FCE
1848
1849 // put library into the queue if not already there
1850 if (find (session.files.begin(), session.files.end(), f)
84e5ea0f
FCE
1851 == session.files.end())
1852 session.files.push_back (f);
57148ee7 1853
84e5ea0f
FCE
1854 return g;
1855 }
1856 }
2b066ec1
FCE
1857 }
1858
2b066ec1 1859 return 0;
2b066ec1
FCE
1860}
1861
1862
57148ee7 1863functiondecl*
2b066ec1
FCE
1864symresolution_info::find_function (const string& name, unsigned arity)
1865{
f76427a2
FCE
1866 // the common path
1867 if (session.functions.find(name) != session.functions.end())
2b066ec1 1868 {
f76427a2
FCE
1869 functiondecl* fd = session.functions[name];
1870 assert (fd->name == name);
1871 if (fd->formal_args.size() == arity)
2b066ec1
FCE
1872 return fd;
1873 }
1874
1875 // search library globals
1876 for (unsigned i=0; i<session.library_files.size(); i++)
1877 {
1878 stapfile* f = session.library_files[i];
1879 for (unsigned j=0; j<f->functions.size(); j++)
1880 if (f->functions[j]->name == name &&
1881 f->functions[j]->formal_args.size() == arity)
1882 {
1883 // put library into the queue if not already there
1884 if (0) // session.verbose_resolution
1885 cerr << " function " << name << " "
1886 << "is defined from " << f->name << endl;
1887
57148ee7 1888 if (find (session.files.begin(), session.files.end(), f)
2b066ec1
FCE
1889 == session.files.end())
1890 session.files.push_back (f);
1891 // else .. print different message?
1892
1893 return f->functions[j];
1894 }
1895 }
1896
1897 return 0;
2b066ec1
FCE
1898}
1899
1900
cbfbbf69
FCE
1901
1902// ------------------------------------------------------------------------
1903// optimization
1904
1905
1906// Do away with functiondecls that are never (transitively) called
1907// from probes.
1908void semantic_pass_opt1 (systemtap_session& s, bool& relaxed_p)
1909{
1910 functioncall_traversing_visitor ftv;
1911 for (unsigned i=0; i<s.probes.size(); i++)
5d23847d
FCE
1912 {
1913 s.probes[i]->body->visit (& ftv);
1914 if (s.probes[i]->sole_location()->condition)
1915 s.probes[i]->sole_location()->condition->visit (& ftv);
1916 }
f76427a2
FCE
1917 vector<functiondecl*> new_unused_functions;
1918 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
cbfbbf69 1919 {
f76427a2
FCE
1920 functiondecl* fd = it->second;
1921 if (ftv.traversed.find(fd) == ftv.traversed.end())
cbfbbf69 1922 {
f76427a2 1923 if (fd->tok->location.file == s.user_file->name && // !tapset
a9e8f7e0 1924 ! s.suppress_warnings)
f76427a2 1925 s.print_warning ("eliding unused function '" + fd->name + "'", fd->tok);
a9e8f7e0 1926 else if (s.verbose>2)
f76427a2 1927 clog << "Eliding unused function " << fd->name
cbfbbf69 1928 << endl;
f76427a2
FCE
1929 // s.functions.erase (it); // NB: can't, since we're already iterating upon it
1930 new_unused_functions.push_back (fd);
cbfbbf69 1931 relaxed_p = false;
cbfbbf69 1932 }
f76427a2
FCE
1933 }
1934 for (unsigned i=0; i<new_unused_functions.size(); i++)
1935 {
1936 map<string,functiondecl*>::iterator where = s.functions.find (new_unused_functions[i]->name);
1937 assert (where != s.functions.end());
1938 s.functions.erase (where);
1939 if (s.tapset_compile_coverage)
1940 s.unused_functions.push_back (new_unused_functions[i]);
cbfbbf69
FCE
1941 }
1942}
1943
1944
1945// ------------------------------------------------------------------------
1946
1947// Do away with local & global variables that are never
1948// written nor read.
cfd621bc 1949void semantic_pass_opt2 (systemtap_session& s, bool& relaxed_p, unsigned iterations)
cbfbbf69
FCE
1950{
1951 varuse_collecting_visitor vut;
cfd621bc 1952
cbfbbf69 1953 for (unsigned i=0; i<s.probes.size(); i++)
5d23847d
FCE
1954 {
1955 s.probes[i]->body->visit (& vut);
1956
1957 if (s.probes[i]->sole_location()->condition)
1958 s.probes[i]->sole_location()->condition->visit (& vut);
1959 }
1960
cbfbbf69
FCE
1961 // NB: Since varuse_collecting_visitor also traverses down
1962 // actually called functions, we don't need to explicitly
1963 // iterate over them. Uncalled ones should have been pruned
1964 // in _opt1 above.
1965 //
1966 // for (unsigned i=0; i<s.functions.size(); i++)
1967 // s.functions[i]->body->visit (& vut);
57148ee7 1968
cbfbbf69 1969 // Now in vut.read/written, we have a mixture of all locals, globals
57148ee7
FCE
1970
1971 for (unsigned i=0; i<s.probes.size(); i++)
cbfbbf69
FCE
1972 for (unsigned j=0; j<s.probes[i]->locals.size(); /* see below */)
1973 {
1974 vardecl* l = s.probes[i]->locals[j];
a9e8f7e0 1975
cbfbbf69
FCE
1976 if (vut.read.find (l) == vut.read.end() &&
1977 vut.written.find (l) == vut.written.end())
1978 {
a9e8f7e0
FCE
1979 if (l->tok->location.file == s.user_file->name && // !tapset
1980 ! s.suppress_warnings)
cfd621bc 1981 s.print_warning ("eliding unused variable '" + l->name + "'", l->tok);
a9e8f7e0 1982 else if (s.verbose>2)
cbfbbf69 1983 clog << "Eliding unused local variable "
1b07c728 1984 << l->name << " in " << s.probes[i]->name << endl;
c3a3c0c9
WC
1985 if (s.tapset_compile_coverage) {
1986 s.probes[i]->unused_locals.push_back
1987 (s.probes[i]->locals[j]);
1988 }
cbfbbf69
FCE
1989 s.probes[i]->locals.erase(s.probes[i]->locals.begin() + j);
1990 relaxed_p = false;
1991 // don't increment j
1992 }
1993 else
27d24ae2
FCE
1994 {
1995 if (vut.written.find (l) == vut.written.end())
cfd621bc 1996 if (iterations == 0 && ! s.suppress_warnings)
6643650d
SC
1997 {
1998 stringstream o;
1999 vector<vardecl*>::iterator it;
cfd621bc
FCE
2000 for (it = s.probes[i]->locals.begin(); it != s.probes[i]->locals.end(); it++)
2001 if (l->name != (*it)->name)
2002 o << " " << (*it)->name;
2003 for (it = s.globals.begin(); it != s.globals.end(); it++)
2004 if (l->name != (*it)->name)
2005 o << " " << (*it)->name;
2006
2007 s.print_warning ("read-only local variable '" + l->name + "' " +
2008 (o.str() == "" ? "" : ("(alternatives:" + o.str() + ")")), l->tok);
6643650d 2009 }
27d24ae2
FCE
2010 j++;
2011 }
cbfbbf69 2012 }
57148ee7 2013
f76427a2
FCE
2014 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
2015 {
2016 functiondecl *fd = it->second;
2017 for (unsigned j=0; j<fd->locals.size(); /* see below */)
2018 {
2019 vardecl* l = fd->locals[j];
2020 if (vut.read.find (l) == vut.read.end() &&
2021 vut.written.find (l) == vut.written.end())
2022 {
2023 if (l->tok->location.file == s.user_file->name && // !tapset
2024 ! s.suppress_warnings)
2025 s.print_warning ("eliding unused variable '" + l->name + "'", l->tok);
2026 else if (s.verbose>2)
2027 clog << "Eliding unused local variable "
2028 << l->name << " in function " << fd->name
2029 << endl;
2030 if (s.tapset_compile_coverage) {
2031 fd->unused_locals.push_back (fd->locals[j]);
2032 }
2033 fd->locals.erase(fd->locals.begin() + j);
2034 relaxed_p = false;
2035 // don't increment j
2036 }
2037 else
2038 {
2039 if (vut.written.find (l) == vut.written.end())
2040 if (iterations == 0 && ! s.suppress_warnings)
2041 {
2042 stringstream o;
2043 vector<vardecl*>::iterator it;
2044 for (it = fd->formal_args.begin() ;
2045 it != fd->formal_args.end(); it++)
2046 if (l->name != (*it)->name)
2047 o << " " << (*it)->name;
2048 for (it = fd->locals.begin(); it != fd->locals.end(); it++)
2049 if (l->name != (*it)->name)
2050 o << " " << (*it)->name;
2051 for (it = s.globals.begin(); it != s.globals.end(); it++)
2052 if (l->name != (*it)->name)
2053 o << " " << (*it)->name;
2054
2055 s.print_warning ("read-only local variable '" + l->name + "' " +
2056 (o.str() == "" ? "" : ("(alternatives:" + o.str() + ")")), l->tok);
2057 }
cfd621bc 2058
f76427a2
FCE
2059 j++;
2060 }
2061 }
2062 }
cbfbbf69
FCE
2063 for (unsigned i=0; i<s.globals.size(); /* see below */)
2064 {
2065 vardecl* l = s.globals[i];
2066 if (vut.read.find (l) == vut.read.end() &&
2067 vut.written.find (l) == vut.written.end())
2068 {
a9e8f7e0
FCE
2069 if (l->tok->location.file == s.user_file->name && // !tapset
2070 ! s.suppress_warnings)
f76427a2 2071 s.print_warning ("eliding unused variable '" + l->name + "'", l->tok);
a9e8f7e0 2072 else if (s.verbose>2)
cbfbbf69
FCE
2073 clog << "Eliding unused global variable "
2074 << l->name << endl;
c3a3c0c9 2075 if (s.tapset_compile_coverage) {
0a102c82 2076 s.unused_globals.push_back(s.globals[i]);
c3a3c0c9 2077 }
0a102c82
SC
2078 s.globals.erase(s.globals.begin() + i);
2079 relaxed_p = false;
2080 // don't increment i
cbfbbf69
FCE
2081 }
2082 else
27d24ae2 2083 {
cfd621bc
FCE
2084 if (vut.written.find (l) == vut.written.end() && ! l->init) // no initializer
2085 if (iterations == 0 && ! s.suppress_warnings)
2086 {
2087 stringstream o;
2088 vector<vardecl*>::iterator it;
2089 for (it = s.globals.begin(); it != s.globals.end(); it++)
2090 if (l->name != (*it)->name)
2091 o << " " << (*it)->name;
57148ee7 2092
cfd621bc
FCE
2093 s.print_warning ("read-only global variable '" + l->name + "' " +
2094 (o.str() == "" ? "" : ("(alternatives:" + o.str() + ")")), l->tok);
2095 }
2096
27d24ae2
FCE
2097 i++;
2098 }
cbfbbf69
FCE
2099 }
2100}
2101
2102
2103// ------------------------------------------------------------------------
2104
8bda6498 2105struct dead_assignment_remover: public update_visitor
cbfbbf69
FCE
2106{
2107 systemtap_session& session;
2108 bool& relaxed_p;
2109 const varuse_collecting_visitor& vut;
cbfbbf69
FCE
2110
2111 dead_assignment_remover(systemtap_session& s, bool& r,
57148ee7 2112 const varuse_collecting_visitor& v):
8bda6498 2113 session(s), relaxed_p(r), vut(v) {}
cbfbbf69
FCE
2114
2115 void visit_assignment (assignment* e);
2116};
2117
2118
cbfbbf69
FCE
2119void
2120dead_assignment_remover::visit_assignment (assignment* e)
2121{
8bda6498
JS
2122 e->left = require (e->left);
2123 e->right = require (e->right);
2124
cbfbbf69 2125 symbol* left = get_symbol_within_expression (e->left);
b0be9bdb 2126 vardecl* leftvar = left->referent; // NB: may be 0 for unresolved $target
8bda6498 2127 if (leftvar) // not unresolved $target, so intended sideeffect may be elided
cbfbbf69 2128 {
cbfbbf69
FCE
2129 if (vut.read.find(leftvar) == vut.read.end()) // var never read?
2130 {
cf9ff341
FCE
2131 // NB: Not so fast! The left side could be an array whose
2132 // index expressions may have side-effects. This would be
57148ee7 2133 // OK if we could replace the array assignment with a
cf9ff341
FCE
2134 // statement-expression containing all the index expressions
2135 // and the rvalue... but we can't.
0a102c82
SC
2136 // Another possibility is that we have an unread global variable
2137 // which are kept for probe end value display.
2138
2139 bool is_global = false;
2140 vector<vardecl*>::iterator it;
2141 for (it = session.globals.begin(); it != session.globals.end(); it++)
2142 if (leftvar->name == (*it)->name)
2143 {
2144 is_global = true;
2145 break;
2146 }
cf9ff341 2147
8bda6498
JS
2148 varuse_collecting_visitor lvut;
2149 e->left->visit (& lvut);
2150 if (lvut.side_effect_free () && !is_global) // XXX: use _wrt() once we track focal_vars
cf9ff341 2151 {
a9e8f7e0
FCE
2152 /* PR 1119: NB: This is not necessary here. A write-only
2153 variable will also be elided soon at the next _opt2 iteration.
2154 if (e->left->tok->location.file == session.user_file->name && // !tapset
2155 ! session.suppress_warnings)
2156 clog << "WARNING: eliding write-only " << *e->left->tok << endl;
2157 else
2158 */
cf9ff341 2159 if (session.verbose>2)
57148ee7 2160 clog << "Eliding assignment to " << leftvar->name
cf9ff341 2161 << " at " << *e->tok << endl;
57148ee7 2162
8bda6498 2163 provide (e->right); // goodbye assignment*
cf9ff341 2164 relaxed_p = false;
8bda6498 2165 return;
cf9ff341 2166 }
cbfbbf69
FCE
2167 }
2168 }
8bda6498 2169 provide (e);
e7625481 2170}
cbfbbf69
FCE
2171
2172// Let's remove assignments to variables that are never read. We
2173// rewrite "(foo = expr)" as "(expr)". This makes foo a candidate to
2174// be optimized away as an unused variable, and expr a candidate to be
2175// removed as a side-effect-free statement expression. Wahoo!
2176void semantic_pass_opt3 (systemtap_session& s, bool& relaxed_p)
2177{
2178 // Recompute the varuse data, which will probably match the opt2
2179 // copy of the computation, except for those totally unused
2180 // variables that opt2 removed.
2181 varuse_collecting_visitor vut;
2182 for (unsigned i=0; i<s.probes.size(); i++)
2183 s.probes[i]->body->visit (& vut); // includes reachable functions too
2184
2185 dead_assignment_remover dar (s, relaxed_p, vut);
2186 // This instance may be reused for multiple probe/function body trims.
2187
2188 for (unsigned i=0; i<s.probes.size(); i++)
8bda6498
JS
2189 s.probes[i]->body = dar.require (s.probes[i]->body);
2190 for (map<string,functiondecl*>::iterator it = s.functions.begin();
2191 it != s.functions.end(); it++)
2192 it->second->body = dar.require (it->second->body);
cbfbbf69 2193 // The rewrite operation is performed within the visitor.
27d24ae2
FCE
2194
2195 // XXX: we could also zap write-only globals here
cbfbbf69
FCE
2196}
2197
2198
2199// ------------------------------------------------------------------------
2200
1cd151d5 2201struct dead_stmtexpr_remover: public update_visitor
cbfbbf69
FCE
2202{
2203 systemtap_session& session;
2204 bool& relaxed_p;
1b07c728 2205 set<vardecl*> focal_vars; // vars considered subject to side-effects
cbfbbf69 2206
57148ee7 2207 dead_stmtexpr_remover(systemtap_session& s, bool& r):
1cd151d5 2208 session(s), relaxed_p(r) {}
cbfbbf69
FCE
2209
2210 void visit_block (block *s);
ba6f838d 2211 void visit_null_statement (null_statement *s);
739a3e81
FCE
2212 void visit_if_statement (if_statement* s);
2213 void visit_foreach_loop (foreach_loop *s);
2214 void visit_for_loop (for_loop *s);
cbfbbf69
FCE
2215 // XXX: and other places where stmt_expr's might be nested
2216
2217 void visit_expr_statement (expr_statement *s);
2218};
2219
2220
ba6f838d
FCE
2221void
2222dead_stmtexpr_remover::visit_null_statement (null_statement *s)
2223{
2224 // easy!
2225 if (session.verbose>2)
2226 clog << "Eliding side-effect-free null statement " << *s->tok << endl;
1cd151d5
JS
2227 s = 0;
2228 provide (s);
ba6f838d
FCE
2229}
2230
2231
cbfbbf69
FCE
2232void
2233dead_stmtexpr_remover::visit_block (block *s)
2234{
ba6f838d
FCE
2235 vector<statement*> new_stmts;
2236 for (unsigned i=0; i<s->statements.size(); i++ )
cbfbbf69 2237 {
1cd151d5
JS
2238 statement* new_stmt = require (s->statements[i], true);
2239 if (new_stmt != 0)
bea72737
JS
2240 {
2241 // flatten nested blocks into this one
1cd151d5 2242 block *b = dynamic_cast<block *>(new_stmt);
bea72737
JS
2243 if (b)
2244 {
2245 if (session.verbose>2)
2246 clog << "Flattening nested block " << *b->tok << endl;
2247 new_stmts.insert(new_stmts.end(),
2248 b->statements.begin(), b->statements.end());
2249 relaxed_p = false;
2250 }
2251 else
1cd151d5 2252 new_stmts.push_back (new_stmt);
bea72737 2253 }
cbfbbf69 2254 }
ba6f838d
FCE
2255 if (new_stmts.size() == 0)
2256 {
2257 if (session.verbose>2)
2258 clog << "Eliding side-effect-free empty block " << *s->tok << endl;
1cd151d5 2259 s = 0;
ba6f838d
FCE
2260 }
2261 else if (new_stmts.size() == 1)
2262 {
2263 if (session.verbose>2)
2264 clog << "Eliding side-effect-free singleton block " << *s->tok << endl;
1cd151d5
JS
2265 provide (new_stmts[0]);
2266 return;
ba6f838d
FCE
2267 }
2268 else
1cd151d5
JS
2269 s->statements = new_stmts;
2270 provide (s);
cbfbbf69
FCE
2271}
2272
739a3e81
FCE
2273void
2274dead_stmtexpr_remover::visit_if_statement (if_statement *s)
2275{
1cd151d5
JS
2276 s->thenblock = require (s->thenblock, true);
2277 s->elseblock = require (s->elseblock, true);
ba6f838d 2278
bea72737 2279 if (s->thenblock == 0)
ba6f838d 2280 {
bea72737
JS
2281 if (s->elseblock == 0)
2282 {
2283 // We may be able to elide this statement, if the condition
2284 // expression is side-effect-free.
2285 varuse_collecting_visitor vct;
2286 s->condition->visit(& vct);
2287 if (vct.side_effect_free ())
2288 {
2289 if (session.verbose>2)
2290 clog << "Eliding side-effect-free if statement "
2291 << *s->tok << endl;
1cd151d5 2292 s = 0; // yeah, baby
bea72737
JS
2293 }
2294 else
2295 {
2296 // We can still turn it into a simple expr_statement though...
2297 if (session.verbose>2)
2298 clog << "Creating simple evaluation from if statement "
2299 << *s->tok << endl;
2300 expr_statement *es = new expr_statement;
2301 es->value = s->condition;
2302 es->tok = es->value->tok;
1cd151d5
JS
2303 provide (es);
2304 return;
bea72737
JS
2305 }
2306 }
2307 else
ba6f838d 2308 {
bea72737
JS
2309 // For an else without a then, we can invert the condition logic to
2310 // avoid having a null statement in the thenblock
ba6f838d 2311 if (session.verbose>2)
bea72737
JS
2312 clog << "Inverting the condition of if statement "
2313 << *s->tok << endl;
2314 unary_expression *ue = new unary_expression;
2315 ue->operand = s->condition;
2316 ue->tok = ue->operand->tok;
2317 ue->op = "!";
2318 s->condition = ue;
2319 s->thenblock = s->elseblock;
2320 s->elseblock = 0;
ba6f838d
FCE
2321 }
2322 }
1cd151d5 2323 provide (s);
739a3e81
FCE
2324}
2325
2326void
2327dead_stmtexpr_remover::visit_foreach_loop (foreach_loop *s)
2328{
1cd151d5 2329 s->block = require(s->block, true);
ba6f838d
FCE
2330
2331 if (s->block == 0)
2332 {
2333 if (session.verbose>2)
2334 clog << "Eliding side-effect-free foreach statement " << *s->tok << endl;
1cd151d5 2335 s = 0; // yeah, baby
ba6f838d 2336 }
1cd151d5 2337 provide (s);
739a3e81
FCE
2338}
2339
2340void
2341dead_stmtexpr_remover::visit_for_loop (for_loop *s)
2342{
1cd151d5 2343 s->block = require(s->block, true);
ba6f838d
FCE
2344
2345 if (s->block == 0)
2346 {
2347 // We may be able to elide this statement, if the condition
2348 // expression is side-effect-free.
2349 varuse_collecting_visitor vct;
2350 if (s->init) s->init->visit(& vct);
2351 s->cond->visit(& vct);
2352 if (s->incr) s->incr->visit(& vct);
2353 if (vct.side_effect_free ())
2354 {
2355 if (session.verbose>2)
2356 clog << "Eliding side-effect-free for statement " << *s->tok << endl;
1cd151d5
JS
2357 s = 0; // yeah, baby
2358 }
2359 else
2360 {
2361 // Can't elide this whole statement; put a null in there.
2362 s->block = new null_statement();
2363 s->block->tok = s->tok;
ba6f838d 2364 }
ba6f838d 2365 }
1cd151d5 2366 provide (s);
739a3e81
FCE
2367}
2368
2369
cbfbbf69
FCE
2370
2371void
2372dead_stmtexpr_remover::visit_expr_statement (expr_statement *s)
2373{
2374 // Run a varuse query against the operand expression. If it has no
2375 // side-effects, replace the entire statement expression by a null
1cd151d5 2376 // statement with the provide() call.
cbfbbf69
FCE
2377 //
2378 // Unlike many other visitors, we do *not* traverse this outermost
2379 // one into the expression subtrees. There is no need - no
2380 // expr_statement nodes will be found there. (Function bodies
2381 // need to be visited explicitly by our caller.)
2382 //
2383 // NB. While we don't share nodes in the parse tree, let's not
2384 // deallocate *s anyway, just in case...
2385
2386 varuse_collecting_visitor vut;
2387 s->value->visit (& vut);
57148ee7 2388
1cd151d5 2389 if (vut.side_effect_free_wrt (focal_vars))
cbfbbf69 2390 {
a9e8f7e0
FCE
2391 /* PR 1119: NB: this message is not a good idea here. It can
2392 name some arbitrary RHS expression of an assignment.
2393 if (s->value->tok->location.file == session.user_file->name && // not tapset
2394 ! session.suppress_warnings)
2395 clog << "WARNING: eliding read-only " << *s->value->tok << endl;
57148ee7 2396 else
a9e8f7e0 2397 */
b0ee93c4 2398 if (session.verbose>2)
cbfbbf69
FCE
2399 clog << "Eliding side-effect-free expression "
2400 << *s->tok << endl;
2401
ba6f838d
FCE
2402 // NB: this 0 pointer is invalid to leave around for any length of
2403 // time, but the parent parse tree objects above handle it.
1cd151d5 2404 s = 0;
cbfbbf69
FCE
2405 relaxed_p = false;
2406 }
1cd151d5 2407 provide (s);
cbfbbf69
FCE
2408}
2409
2410
2411void semantic_pass_opt4 (systemtap_session& s, bool& relaxed_p)
2412{
2413 // Finally, let's remove some statement-expressions that have no
2414 // side-effect. These should be exactly those whose private varuse
2415 // visitors come back with an empty "written" and "embedded" lists.
57148ee7 2416
cbfbbf69
FCE
2417 dead_stmtexpr_remover duv (s, relaxed_p);
2418 // This instance may be reused for multiple probe/function body trims.
2419
2420 for (unsigned i=0; i<s.probes.size(); i++)
1b07c728 2421 {
f76427a2
FCE
2422 if (pending_interrupts) break;
2423
ba6f838d
FCE
2424 derived_probe* p = s.probes[i];
2425
1b07c728
FCE
2426 duv.focal_vars.clear ();
2427 duv.focal_vars.insert (s.globals.begin(),
2428 s.globals.end());
ba6f838d
FCE
2429 duv.focal_vars.insert (p->locals.begin(),
2430 p->locals.end());
2431
1cd151d5 2432 p->body = duv.require(p->body, true);
ba6f838d
FCE
2433 if (p->body == 0)
2434 {
2435 if (! s.suppress_warnings)
cfd621bc 2436 s.print_warning ("side-effect-free probe '" + p->name + "'", p->tok);
ba6f838d
FCE
2437
2438 p->body = new null_statement();
2439 p->body->tok = p->tok;
27d24ae2
FCE
2440
2441 // XXX: possible duplicate warnings; see below
ba6f838d 2442 }
1b07c728 2443 }
f76427a2 2444 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
1b07c728 2445 {
f76427a2
FCE
2446 if (pending_interrupts) break;
2447
2448 functiondecl* fn = it->second;
1b07c728 2449 duv.focal_vars.clear ();
ba6f838d
FCE
2450 duv.focal_vars.insert (fn->locals.begin(),
2451 fn->locals.end());
2452 duv.focal_vars.insert (fn->formal_args.begin(),
2453 fn->formal_args.end());
1b07c728
FCE
2454 duv.focal_vars.insert (s.globals.begin(),
2455 s.globals.end());
ba6f838d 2456
1cd151d5 2457 fn->body = duv.require(fn->body, true);
ba6f838d
FCE
2458 if (fn->body == 0)
2459 {
2460 if (! s.suppress_warnings)
cfd621bc 2461 s.print_warning ("side-effect-free function '" + fn->name + "'", fn->tok);
ba6f838d
FCE
2462
2463 fn->body = new null_statement();
2464 fn->body->tok = fn->tok;
27d24ae2
FCE
2465
2466 // XXX: the next iteration of the outer optimization loop may
2467 // take this new null_statement away again, and thus give us a
2468 // fresh warning. It would be better if this fixup was performed
2469 // only after the relaxation iterations.
2470 // XXX: or else see bug #6469.
ba6f838d 2471 }
1b07c728 2472 }
cbfbbf69
FCE
2473}
2474
bea72737
JS
2475
2476// ------------------------------------------------------------------------
2477
2478// The goal of this visitor is to reduce top-level expressions in void context
2479// into separate statements that evaluate each subcomponent of the expression.
2480// The dead-statement-remover can later remove some parts if they have no side
2481// effects.
9f9873df
JS
2482//
2483// All expressions must be overridden here so we never visit their subexpressions
2484// accidentally. Thus, the only visited expressions should be value of an
2485// expr_statement.
2486//
2487// For an expression to replace its expr_statement with something else, it will
2488// let the new statement provide(), and then provide(0) for itself. The
2489// expr_statement will take this as a sign that it's been replaced.
2490struct void_statement_reducer: public update_visitor
bea72737
JS
2491{
2492 systemtap_session& session;
2493 bool& relaxed_p;
bea72737
JS
2494 set<vardecl*> focal_vars; // vars considered subject to side-effects
2495
2496 void_statement_reducer(systemtap_session& s, bool& r):
9f9873df 2497 session(s), relaxed_p(r) {}
bea72737 2498
133c7b1d 2499 void visit_expr_statement (expr_statement* s);
9f9873df
JS
2500
2501 // expressions in conditional / loop controls are definitely a side effect,
2502 // but still recurse into the child statements
bea72737
JS
2503 void visit_if_statement (if_statement* s);
2504 void visit_for_loop (for_loop* s);
2505 void visit_foreach_loop (foreach_loop* s);
2506
2507 // these expressions get rewritten into their statement equivalents
2508 void visit_logical_or_expr (logical_or_expr* e);
2509 void visit_logical_and_expr (logical_and_expr* e);
2510 void visit_ternary_expression (ternary_expression* e);
2511
2512 // all of these can be reduced into simpler statements
2513 void visit_binary_expression (binary_expression* e);
2514 void visit_unary_expression (unary_expression* e);
2515 void visit_comparison (comparison* e);
2516 void visit_concatenation (concatenation* e);
2517 void visit_functioncall (functioncall* e);
2518 void visit_print_format (print_format* e);
9b5af295 2519 void visit_cast_op (cast_op* e);
bea72737
JS
2520
2521 // these are a bit hairy to grok due to the intricacies of indexables and
2522 // stats, so I'm chickening out and skipping them...
9f9873df
JS
2523 void visit_array_in (array_in* e) { provide (e); }
2524 void visit_arrayindex (arrayindex* e) { provide (e); }
2525 void visit_stat_op (stat_op* e) { provide (e); }
2526 void visit_hist_op (hist_op* e) { provide (e); }
bea72737
JS
2527
2528 // these can't be reduced because they always have an effect
9f9873df
JS
2529 void visit_return_statement (return_statement* s) { provide (s); }
2530 void visit_delete_statement (delete_statement* s) { provide (s); }
2531 void visit_pre_crement (pre_crement* e) { provide (e); }
2532 void visit_post_crement (post_crement* e) { provide (e); }
2533 void visit_assignment (assignment* e) { provide (e); }
bea72737
JS
2534};
2535
2536
133c7b1d
JS
2537void
2538void_statement_reducer::visit_expr_statement (expr_statement* s)
2539{
9f9873df 2540 s->value = require (s->value, true);
133c7b1d 2541
9f9873df
JS
2542 // if the expression provides 0, that's our signal that a new
2543 // statement has been provided, so we shouldn't provide this one.
2544 if (s->value != 0)
2545 provide(s);
bea72737
JS
2546}
2547
2548void
2549void_statement_reducer::visit_if_statement (if_statement* s)
2550{
9f9873df
JS
2551 // s->condition is never void
2552 s->thenblock = require (s->thenblock);
2553 s->elseblock = require (s->elseblock);
2554 provide (s);
bea72737
JS
2555}
2556
2557void
2558void_statement_reducer::visit_for_loop (for_loop* s)
2559{
9f9873df
JS
2560 // s->init/cond/incr are never void
2561 s->block = require (s->block);
2562 provide (s);
bea72737
JS
2563}
2564
2565void
2566void_statement_reducer::visit_foreach_loop (foreach_loop* s)
2567{
9f9873df
JS
2568 // s->indexes/base/limit are never void
2569 s->block = require (s->block);
2570 provide (s);
bea72737
JS
2571}
2572
2573void
2574void_statement_reducer::visit_logical_or_expr (logical_or_expr* e)
2575{
2576 // In void context, the evaluation of "a || b" is exactly like
2577 // "if (!a) b", so let's do that instead.
2578
bea72737
JS
2579 if (session.verbose>2)
2580 clog << "Creating if statement from unused logical-or "
2581 << *e->tok << endl;
2582
2583 if_statement *is = new if_statement;
2584 is->tok = e->tok;
2585 is->elseblock = 0;
bea72737
JS
2586
2587 unary_expression *ue = new unary_expression;
2588 ue->operand = e->left;
2589 ue->tok = e->tok;
2590 ue->op = "!";
2591 is->condition = ue;
2592
133c7b1d 2593 expr_statement *es = new expr_statement;
bea72737
JS
2594 es->value = e->right;
2595 es->tok = es->value->tok;
2596 is->thenblock = es;
2597
2598 is->visit(this);
2599 relaxed_p = false;
9f9873df
JS
2600 e = 0;
2601 provide (e);
bea72737
JS
2602}
2603
2604void
2605void_statement_reducer::visit_logical_and_expr (logical_and_expr* e)
2606{
2607 // In void context, the evaluation of "a && b" is exactly like
2608 // "if (a) b", so let's do that instead.
2609
bea72737
JS
2610 if (session.verbose>2)
2611 clog << "Creating if statement from unused logical-and "
2612 << *e->tok << endl;
2613
2614 if_statement *is = new if_statement;
2615 is->tok = e->tok;
2616 is->elseblock = 0;
2617 is->condition = e->left;
bea72737 2618
133c7b1d 2619 expr_statement *es = new expr_statement;
bea72737
JS
2620 es->value = e->right;
2621 es->tok = es->value->tok;
2622 is->thenblock = es;
2623
2624 is->visit(this);
2625 relaxed_p = false;
9f9873df
JS
2626 e = 0;
2627 provide (e);
bea72737
JS
2628}
2629
2630void
2631void_statement_reducer::visit_ternary_expression (ternary_expression* e)
2632{
2633 // In void context, the evaluation of "a ? b : c" is exactly like
2634 // "if (a) b else c", so let's do that instead.
2635
bea72737
JS
2636 if (session.verbose>2)
2637 clog << "Creating if statement from unused ternary expression "
2638 << *e->tok << endl;
2639
2640 if_statement *is = new if_statement;
2641 is->tok = e->tok;
2642 is->condition = e->cond;
bea72737 2643
133c7b1d 2644 expr_statement *es = new expr_statement;
bea72737
JS
2645 es->value = e->truevalue;
2646 es->tok = es->value->tok;
2647 is->thenblock = es;
2648
2649 es = new expr_statement;
2650 es->value = e->falsevalue;
2651 es->tok = es->value->tok;
2652 is->elseblock = es;
2653
2654 is->visit(this);
2655 relaxed_p = false;
9f9873df
JS
2656 e = 0;
2657 provide (e);
bea72737
JS
2658}
2659
2660void
2661void_statement_reducer::visit_binary_expression (binary_expression* e)
2662{
2663 // When the result of a binary operation isn't needed, it's just as good to
2664 // evaluate the operands as sequential statements in a block.
2665
bea72737
JS
2666 if (session.verbose>2)
2667 clog << "Eliding unused binary " << *e->tok << endl;
2668
2669 block *b = new block;
9f9873df 2670 b->tok = e->tok;
bea72737 2671
133c7b1d 2672 expr_statement *es = new expr_statement;
bea72737
JS
2673 es->value = e->left;
2674 es->tok = es->value->tok;
2675 b->statements.push_back(es);
2676
2677 es = new expr_statement;
2678 es->value = e->right;
2679 es->tok = es->value->tok;
2680 b->statements.push_back(es);
2681
2682 b->visit(this);
2683 relaxed_p = false;
9f9873df
JS
2684 e = 0;
2685 provide (e);
bea72737
JS
2686}
2687
2688void
2689void_statement_reducer::visit_unary_expression (unary_expression* e)
2690{
2691 // When the result of a unary operation isn't needed, it's just as good to
2692 // evaluate the operand directly
2693
bea72737
JS
2694 if (session.verbose>2)
2695 clog << "Eliding unused unary " << *e->tok << endl;
2696
bea72737 2697 relaxed_p = false;
9f9873df 2698 e->operand->visit(this);
bea72737
JS
2699}
2700
2701void
2702void_statement_reducer::visit_comparison (comparison* e)
2703{
2704 visit_binary_expression(e);
2705}
2706
2707void
2708void_statement_reducer::visit_concatenation (concatenation* e)
2709{
2710 visit_binary_expression(e);
2711}
2712
2713void
2714void_statement_reducer::visit_functioncall (functioncall* e)
2715{
2716 // If a function call is pure and its result ignored, we can elide the call
c7d47935 2717 // and just evaluate the arguments in sequence
bea72737
JS
2718
2719 if (!e->args.size())
9f9873df
JS
2720 {
2721 provide (e);
2722 return;
2723 }
bea72737
JS
2724
2725 varuse_collecting_visitor vut;
2726 vut.traversed.insert (e->referent);
2727 vut.current_function = e->referent;
2728 e->referent->body->visit (& vut);
2729 if (!vut.side_effect_free_wrt (focal_vars))
9f9873df
JS
2730 {
2731 provide (e);
2732 return;
2733 }
bea72737
JS
2734
2735 if (session.verbose>2)
2736 clog << "Eliding side-effect-free function call " << *e->tok << endl;
2737
133c7b1d
JS
2738 block *b = new block;
2739 b->tok = e->tok;
bea72737 2740
133c7b1d 2741 for (unsigned i=0; i<e->args.size(); i++ )
bea72737 2742 {
133c7b1d
JS
2743 expr_statement *es = new expr_statement;
2744 es->value = e->args[i];
2745 es->tok = es->value->tok;
bea72737 2746 b->statements.push_back(es);
bea72737
JS
2747 }
2748
133c7b1d 2749 b->visit(this);
bea72737 2750 relaxed_p = false;
9f9873df
JS
2751 e = 0;
2752 provide (e);
bea72737
JS
2753}
2754
2755void
2756void_statement_reducer::visit_print_format (print_format* e)
2757{
2758 // When an sprint's return value is ignored, we can simply evaluate the
2759 // arguments in sequence
2760
2761 if (e->print_to_stream || !e->args.size())
9f9873df
JS
2762 {
2763 provide (e);
2764 return;
2765 }
bea72737
JS
2766
2767 if (session.verbose>2)
2768 clog << "Eliding unused print " << *e->tok << endl;
2769
133c7b1d
JS
2770 block *b = new block;
2771 b->tok = e->tok;
bea72737 2772
133c7b1d 2773 for (unsigned i=0; i<e->args.size(); i++ )
bea72737 2774 {
133c7b1d
JS
2775 expr_statement *es = new expr_statement;
2776 es->value = e->args[i];
2777 es->tok = es->value->tok;
bea72737 2778 b->statements.push_back(es);
bea72737
JS
2779 }
2780
133c7b1d 2781 b->visit(this);
bea72737 2782 relaxed_p = false;
9f9873df
JS
2783 e = 0;
2784 provide (e);
bea72737
JS
2785}
2786
9b5af295
JS
2787void
2788void_statement_reducer::visit_cast_op (cast_op* e)
2789{
2790 // When the result of a cast operation isn't needed, it's just as good to
2791 // evaluate the operand directly
2792
2793 if (session.verbose>2)
2794 clog << "Eliding unused typecast " << *e->tok << endl;
2795
2796 relaxed_p = false;
2797 e->operand->visit(this);
2798}
2799
bea72737
JS
2800
2801void semantic_pass_opt5 (systemtap_session& s, bool& relaxed_p)
2802{
2803 // Let's simplify statements with unused computed values.
2804
2805 void_statement_reducer vuv (s, relaxed_p);
2806 // This instance may be reused for multiple probe/function body trims.
2807
2808 vuv.focal_vars.insert (s.globals.begin(), s.globals.end());
2809
2810 for (unsigned i=0; i<s.probes.size(); i++)
9f9873df
JS
2811 s.probes[i]->body = vuv.require (s.probes[i]->body);
2812 for (map<string,functiondecl*>::iterator it = s.functions.begin();
2813 it != s.functions.end(); it++)
2814 it->second->body = vuv.require (it->second->body);
bea72737
JS
2815}
2816
2817
88bbd60d
DS
2818struct duplicate_function_remover: public functioncall_traversing_visitor
2819{
2820 systemtap_session& s;
88bbd60d
DS
2821 map<functiondecl*, functiondecl*>& duplicate_function_map;
2822
c214bd6a 2823 duplicate_function_remover(systemtap_session& sess,
88bbd60d 2824 map<functiondecl*, functiondecl*>&dfm):
c214bd6a 2825 s(sess), duplicate_function_map(dfm) {};
88bbd60d
DS
2826
2827 void visit_functioncall (functioncall* e);
2828};
2829
2830void
2831duplicate_function_remover::visit_functioncall (functioncall *e)
2832{
2833 functioncall_traversing_visitor::visit_functioncall (e);
2834
2835 // If the current function call reference points to a function that
2836 // is a duplicate, replace it.
2837 if (duplicate_function_map.count(e->referent) != 0)
2838 {
2839 if (s.verbose>2)
2840 clog << "Changing " << e->referent->name
2841 << " reference to "
2842 << duplicate_function_map[e->referent]->name
2843 << " reference\n";
2844 e->tok = duplicate_function_map[e->referent]->tok;
2845 e->function = duplicate_function_map[e->referent]->name;
2846 e->referent = duplicate_function_map[e->referent];
88bbd60d
DS
2847 }
2848}
2849
2850static string
2851get_functionsig (functiondecl* f)
2852{
2853 ostringstream s;
2854
2855 // Get the "name:args body" of the function in s. We have to
2856 // include the args since the function 'x1(a, b)' is different than
2857 // the function 'x2(b, a)' even if the bodies of the two functions
2858 // are exactly the same.
2859 f->printsig(s);
2860 f->body->print(s);
2861
2862 // printsig puts f->name + ':' on the front. Remove this
2863 // (otherwise, functions would never compare equal).
2864 string str = s.str().erase(0, f->name.size() + 1);
2865
2866 // Return the function signature.
2867 return str;
2868}
2869
bea72737 2870void semantic_pass_opt6 (systemtap_session& s, bool& relaxed_p)
88bbd60d
DS
2871{
2872 // Walk through all the functions, looking for duplicates.
2873 map<string, functiondecl*> functionsig_map;
2874 map<functiondecl*, functiondecl*> duplicate_function_map;
f76427a2
FCE
2875
2876
2877 vector<functiondecl*> newly_zapped_functions;
2878 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
88bbd60d 2879 {
f76427a2
FCE
2880 functiondecl *fd = it->second;
2881 string functionsig = get_functionsig(fd);
88bbd60d
DS
2882
2883 if (functionsig_map.count(functionsig) == 0)
c214bd6a
DS
2884 {
2885 // This function is unique. Remember it.
f76427a2 2886 functionsig_map[functionsig] = fd;
c214bd6a 2887 }
88bbd60d 2888 else
c214bd6a
DS
2889 {
2890 // This function is a duplicate.
f76427a2
FCE
2891 duplicate_function_map[fd] = functionsig_map[functionsig];
2892 newly_zapped_functions.push_back (fd);
c214bd6a 2893 relaxed_p = false;
c214bd6a 2894 }
88bbd60d 2895 }
f76427a2
FCE
2896 for (unsigned i=0; i<newly_zapped_functions.size(); i++)
2897 {
2898 map<string,functiondecl*>::iterator where = s.functions.find (newly_zapped_functions[i]->name);
2899 assert (where != s.functions.end());
2900 s.functions.erase (where);
2901 }
2902
88bbd60d
DS
2903
2904 // If we have duplicate functions, traverse down the tree, replacing
2905 // the appropriate function calls.
2906 // duplicate_function_remover::visit_functioncall() handles the
c214bd6a 2907 // details of replacing the function calls.
88bbd60d
DS
2908 if (duplicate_function_map.size() != 0)
2909 {
c214bd6a 2910 duplicate_function_remover dfr (s, duplicate_function_map);
88bbd60d
DS
2911
2912 for (unsigned i=0; i < s.probes.size(); i++)
2913 s.probes[i]->body->visit(&dfr);
2914 }
2915}
2916
cbfbbf69
FCE
2917
2918static int
c214bd6a 2919semantic_pass_optimize1 (systemtap_session& s)
cbfbbf69
FCE
2920{
2921 // In this pass, we attempt to rewrite probe/function bodies to
2922 // eliminate some blatantly unnecessary code. This is run before
2923 // type inference, but after symbol resolution and derived_probe
2924 // creation. We run an outer "relaxation" loop that repeats the
2925 // optimizations until none of them find anything to remove.
2926
2927 int rc = 0;
2928
2929 bool relaxed_p = false;
cfd621bc 2930 unsigned iterations = 0;
cbfbbf69
FCE
2931 while (! relaxed_p)
2932 {
49abf162
FCE
2933 if (pending_interrupts) break;
2934
cbfbbf69
FCE
2935 relaxed_p = true; // until proven otherwise
2936
2937 semantic_pass_opt1 (s, relaxed_p);
cfd621bc 2938 semantic_pass_opt2 (s, relaxed_p, iterations); // produce some warnings only on iteration=0
cbfbbf69
FCE
2939 semantic_pass_opt3 (s, relaxed_p);
2940 semantic_pass_opt4 (s, relaxed_p);
bea72737 2941 semantic_pass_opt5 (s, relaxed_p);
cfd621bc
FCE
2942
2943 iterations ++;
c214bd6a
DS
2944 }
2945
c214bd6a
DS
2946 return rc;
2947}
2948
2949
2950static int
2951semantic_pass_optimize2 (systemtap_session& s)
2952{
2953 // This is run after type inference. We run an outer "relaxation"
2954 // loop that repeats the optimizations until none of them find
2955 // anything to remove.
2956
2957 int rc = 0;
2958
2959 bool relaxed_p = false;
2960 while (! relaxed_p)
2961 {
49abf162 2962 if (pending_interrupts) break;
c214bd6a
DS
2963 relaxed_p = true; // until proven otherwise
2964
bea72737 2965 semantic_pass_opt6 (s, relaxed_p);
cbfbbf69
FCE
2966 }
2967
cbfbbf69
FCE
2968 return rc;
2969}
2970
2971
2972
2b066ec1
FCE
2973// ------------------------------------------------------------------------
2974// type resolution
2975
2976
2977static int
2978semantic_pass_types (systemtap_session& s)
2979{
2980 int rc = 0;
2981
2982 // next pass: type inference
2983 unsigned iterations = 0;
2984 typeresolution_info ti (s);
57148ee7 2985
2b066ec1
FCE
2986 ti.assert_resolvability = false;
2987 // XXX: maybe convert to exception-based error signalling
2988 while (1)
2989 {
49abf162
FCE
2990 if (pending_interrupts) break;
2991
2b066ec1 2992 iterations ++;
2b066ec1
FCE
2993 ti.num_newly_resolved = 0;
2994 ti.num_still_unresolved = 0;
2995
f76427a2 2996 for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
2b066ec1 2997 {
49abf162
FCE
2998 if (pending_interrupts) break;
2999
f76427a2 3000 functiondecl* fd = it->second;
bdef2583 3001 ti.current_probe = 0;
f76427a2 3002 ti.current_function = fd;
8a43522c 3003 ti.t = pe_unknown;
f76427a2 3004 fd->body->visit (& ti);
8a43522c
GH
3005 // NB: we don't have to assert a known type for
3006 // functions here, to permit a "void" function.
3007 // The translator phase will omit the "retvalue".
3008 //
f76427a2
FCE
3009 // if (fd->type == pe_unknown)
3010 // ti.unresolved (fd->tok);
57148ee7 3011 }
2b066ec1
FCE
3012
3013 for (unsigned j=0; j<s.probes.size(); j++)
3014 {
49abf162
FCE
3015 if (pending_interrupts) break;
3016
2b066ec1
FCE
3017 derived_probe* pn = s.probes[j];
3018 ti.current_function = 0;
bdef2583 3019 ti.current_probe = pn;
2b066ec1
FCE
3020 ti.t = pe_unknown;
3021 pn->body->visit (& ti);
5d23847d
FCE
3022
3023 probe_point* pp = pn->sole_location();
3024 if (pp->condition)
3025 {
3026 ti.current_function = 0;
3027 ti.current_probe = 0;
3028 ti.t = pe_long; // NB: expected type
3029 pp->condition->visit (& ti);
3030 }
2b066ec1
FCE
3031 }
3032
3033 for (unsigned j=0; j<s.globals.size(); j++)
3034 {
3035 vardecl* gd = s.globals[j];
3036 if (gd->type == pe_unknown)
3037 ti.unresolved (gd->tok);
3038 }
57148ee7 3039
2b066ec1 3040 if (ti.num_newly_resolved == 0) // converged
78f6bba6
FCE
3041 {
3042 if (ti.num_still_unresolved == 0)
3043 break; // successfully
3044 else if (! ti.assert_resolvability)
3045 ti.assert_resolvability = true; // last pass, with error msgs
3046 else
3047 { // unsuccessful conclusion
3048 rc ++;
3049 break;
3050 }
3051 }
2b066ec1 3052 }
57148ee7 3053
7e41d3dc 3054 return rc + s.num_errors();
2b066ec1
FCE
3055}
3056
3057
bdef2583
FCE
3058
3059typeresolution_info::typeresolution_info (systemtap_session& s):
3060 session(s), current_function(0), current_probe(0)
3061{
3062}
3063
3064
2b066ec1
FCE
3065void
3066typeresolution_info::visit_literal_number (literal_number* e)
3067{
3068 assert (e->type == pe_long);
3069 if ((t == e->type) || (t == pe_unknown))
3070 return;
3071
3072 mismatch (e->tok, e->type, t);
3073}
3074
3075
3076void
3077typeresolution_info::visit_literal_string (literal_string* e)
3078{
3079 assert (e->type == pe_string);
3080 if ((t == e->type) || (t == pe_unknown))
3081 return;
3082
3083 mismatch (e->tok, e->type, t);
3084}
3085
3086
3087void
3088typeresolution_info::visit_logical_or_expr (logical_or_expr *e)
3089{
3090 visit_binary_expression (e);
3091}
3092
3093
3094void
3095typeresolution_info::visit_logical_and_expr (logical_and_expr *e)
3096{
3097 visit_binary_expression (e);
3098}
3099
3100
3101void
3102typeresolution_info::visit_comparison (comparison *e)
3103{
d5d7c2cc 3104 // NB: result of any comparison is an integer!
553d27a5
FCE
3105 if (t == pe_stats || t == pe_string)
3106 invalid (e->tok, t);
3107
3108 t = (e->right->type != pe_unknown) ? e->right->type : pe_unknown;
3109 e->left->visit (this);
3110 t = (e->left->type != pe_unknown) ? e->left->type : pe_unknown;
3111 e->right->visit (this);
57148ee7 3112
553d27a5
FCE
3113 if (e->left->type != pe_unknown &&
3114 e->right->type != pe_unknown &&
3115 e->left->type != e->right->type)
3116 mismatch (e->tok, e->left->type, e->right->type);
57148ee7 3117
553d27a5
FCE
3118 if (e->type == pe_unknown)
3119 {
3120 e->type = pe_long;
3121 resolved (e->tok, e->type);
3122 }
2b066ec1
FCE
3123}
3124
3125
3126void
3127typeresolution_info::visit_concatenation (concatenation *e)
3128{
553d27a5
FCE
3129 if (t != pe_unknown && t != pe_string)
3130 invalid (e->tok, t);
3131
3132 t = pe_string;
3133 e->left->visit (this);
3134 t = pe_string;
3135 e->right->visit (this);
3136
3137 if (e->type == pe_unknown)
3138 {
3139 e->type = pe_string;
3140 resolved (e->tok, e->type);
3141 }
2b066ec1
FCE
3142}
3143
3144
2b066ec1
FCE
3145void
3146typeresolution_info::visit_assignment (assignment *e)
3147{
553d27a5
FCE
3148 if (t == pe_stats)
3149 invalid (e->tok, t);
2b066ec1 3150
2b066ec1
FCE
3151 if (e->op == "<<<") // stats aggregation
3152 {
553d27a5
FCE
3153 if (t == pe_string)
3154 invalid (e->tok, t);
3155
2b066ec1
FCE
3156 t = pe_stats;
3157 e->left->visit (this);
3158 t = pe_long;
3159 e->right->visit (this);
57b73400
GH
3160 if (e->type == pe_unknown ||
3161 e->type == pe_stats)
2b066ec1
FCE
3162 {
3163 e->type = pe_long;
3164 resolved (e->tok, e->type);
3165 }
3166 }
57b73400
GH
3167
3168 else if (e->left->type == pe_stats)
3169 invalid (e->left->tok, e->left->type);
3170
3171 else if (e->right->type == pe_stats)
3172 invalid (e->right->tok, e->right->type);
3173
553d27a5 3174 else if (e->op == "+=" || // numeric only
d5d7c2cc
FCE
3175 e->op == "-=" ||
3176 e->op == "*=" ||
3177 e->op == "/=" ||
3178 e->op == "%=" ||
3179 e->op == "&=" ||
3180 e->op == "^=" ||
3181 e->op == "|=" ||
3182 e->op == "<<=" ||
3183 e->op == ">>=" ||
553d27a5 3184 false)
2b066ec1 3185 {
553d27a5 3186 visit_binary_expression (e);
2b066ec1 3187 }
d5d7c2cc
FCE
3188 else if (e->op == ".=" || // string only
3189 false)
3190 {
3191 if (t == pe_long || t == pe_stats)
3192 invalid (e->tok, t);
3193
3194 t = pe_string;
3195 e->left->visit (this);
3196 t = pe_string;
3197 e->right->visit (this);
3198 if (e->type == pe_unknown)
3199 {
3200 e->type = pe_string;
3201 resolved (e->tok, e->type);
3202 }
3203 }
3204 else if (e->op == "=") // overloaded = for string & numeric operands
2b066ec1 3205 {
553d27a5 3206 // logic similar to ternary_expression
2b066ec1 3207 exp_type sub_type = t;
553d27a5
FCE
3208
3209 // Infer types across the l/r values
3210 if (sub_type == pe_unknown && e->type != pe_unknown)
2b066ec1 3211 sub_type = e->type;
553d27a5
FCE
3212
3213 t = (sub_type != pe_unknown) ? sub_type :
3214 (e->right->type != pe_unknown) ? e->right->type :
3215 pe_unknown;
2b066ec1 3216 e->left->visit (this);
553d27a5
FCE
3217 t = (sub_type != pe_unknown) ? sub_type :
3218 (e->left->type != pe_unknown) ? e->left->type :
3219 pe_unknown;
2b066ec1 3220 e->right->visit (this);
57148ee7 3221
553d27a5 3222 if ((sub_type != pe_unknown) && (e->type == pe_unknown))
2b066ec1
FCE
3223 {
3224 e->type = sub_type;
3225 resolved (e->tok, e->type);
3226 }
553d27a5 3227 if ((sub_type == pe_unknown) && (e->left->type != pe_unknown))
2b066ec1
FCE
3228 {
3229 e->type = e->left->type;
3230 resolved (e->tok, e->type);
3231 }
553d27a5
FCE
3232
3233 if (e->left->type != pe_unknown &&
3234 e->right->type != pe_unknown &&
3235 e->left->type != e->right->type)
3236 mismatch (e->tok, e->left->type, e->right->type);
d02548c0 3237
553d27a5 3238 }
d5d7c2cc
FCE
3239 else
3240 throw semantic_error ("unsupported assignment operator " + e->op);
553d27a5
FCE
3241}
3242
3243
3244void
3245typeresolution_info::visit_binary_expression (binary_expression* e)
3246{
3247 if (t == pe_stats || t == pe_string)
3248 invalid (e->tok, t);
3249
3250 t = pe_long;
3251 e->left->visit (this);
3252 t = pe_long;
3253 e->right->visit (this);
3254
3255 if (e->left->type != pe_unknown &&
3256 e->right->type != pe_unknown &&
3257 e->left->type != e->right->type)
3258 mismatch (e->tok, e->left->type, e->right->type);
57148ee7 3259
553d27a5
FCE
3260 if (e->type == pe_unknown)
3261 {
3262 e->type = pe_long;
3263 resolved (e->tok, e->type);
2b066ec1
FCE
3264 }
3265}
3266
3267
3268void
3269typeresolution_info::visit_pre_crement (pre_crement *e)
3270{
3271 visit_unary_expression (e);
3272}
3273
3274
3275void
3276typeresolution_info::visit_post_crement (post_crement *e)
3277{
3278 visit_unary_expression (e);
3279}
3280
3281
3282void
3283typeresolution_info::visit_unary_expression (unary_expression* e)
3284{
553d27a5
FCE
3285 if (t == pe_stats || t == pe_string)
3286 invalid (e->tok, t);
3287
2b066ec1
FCE
3288 t = pe_long;
3289 e->operand->visit (this);
3290
553d27a5 3291 if (e->type == pe_unknown)
2b066ec1
FCE
3292 {
3293 e->type = pe_long;
3294 resolved (e->tok, e->type);
3295 }
3296}
3297
3298
2b066ec1
FCE
3299void
3300typeresolution_info::visit_ternary_expression (ternary_expression* e)
3301{
3302 exp_type sub_type = t;
3303
3304 t = pe_long;
3305 e->cond->visit (this);
3306
553d27a5 3307 // Infer types across the true/false arms of the ternary expression.
2b066ec1
FCE
3308
3309 if (sub_type == pe_unknown && e->type != pe_unknown)
3310 sub_type = e->type;
3311 t = sub_type;
3312 e->truevalue->visit (this);
3313 t = sub_type;
3314 e->falsevalue->visit (this);
3315
3316 if ((sub_type == pe_unknown) && (e->type != pe_unknown))
3317 ; // already resolved
3318 else if ((sub_type != pe_unknown) && (e->type == pe_unknown))
3319 {
3320 e->type = sub_type;
3321 resolved (e->tok, e->type);
3322 }
3323 else if ((sub_type == pe_unknown) && (e->truevalue->type != pe_unknown))
3324 {
3325 e->type = e->truevalue->type;
3326 resolved (e->tok, e->type);
3327 }
3328 else if ((sub_type == pe_unknown) && (e->falsevalue->type != pe_unknown))
3329 {
3330 e->type = e->falsevalue->type;
3331 resolved (e->tok, e->type);
3332 }
3333 else if (e->type != sub_type)
3334 mismatch (e->tok, sub_type, e->type);
3335}
3336
3337
3338template <class Referrer, class Referent>
3339void resolve_2types (Referrer* referrer, Referent* referent,
31966088 3340 typeresolution_info* r, exp_type t, bool accept_unknown = false)
2b066ec1
FCE
3341{
3342 exp_type& re_type = referrer->type;
3343 const token* re_tok = referrer->tok;
3344 exp_type& te_type = referent->type;
3345 const token* te_tok = referent->tok;
3346
3347 if (t != pe_unknown && re_type == t && re_type == te_type)
3348 ; // do nothing: all three e->types in agreement
3349 else if (t == pe_unknown && re_type != pe_unknown && re_type == te_type)
3350 ; // do nothing: two known e->types in agreement
3351 else if (re_type != pe_unknown && te_type != pe_unknown && re_type != te_type)
3352 r->mismatch (re_tok, re_type, te_type);
3353 else if (re_type != pe_unknown && t != pe_unknown && re_type != t)
3354 r->mismatch (re_tok, re_type, t);
3355 else if (te_type != pe_unknown && t != pe_unknown && te_type != t)
3356 r->mismatch (te_tok, te_type, t);
3357 else if (re_type == pe_unknown && t != pe_unknown)
3358 {
3359 // propagate from upstream
3360 re_type = t;
3361 r->resolved (re_tok, re_type);
3362 // catch re_type/te_type mismatch later
3363 }
3364 else if (re_type == pe_unknown && te_type != pe_unknown)
3365 {
3366 // propagate from referent
3367 re_type = te_type;
3368 r->resolved (re_tok, re_type);
3369 // catch re_type/t mismatch later
3370 }
3371 else if (re_type != pe_unknown && te_type == pe_unknown)
3372 {
3373 // propagate to referent
3374 te_type = re_type;
3375 r->resolved (te_tok, te_type);
3376 // catch re_type/t mismatch later
3377 }
31966088 3378 else if (! accept_unknown)
2b066ec1
FCE
3379 r->unresolved (re_tok);
3380}
3381
3382
3383void
3384typeresolution_info::visit_symbol (symbol* e)
3385{
3386 assert (e->referent != 0);
d02548c0 3387 resolve_2types (e, e->referent, this, t);
2b066ec1
FCE
3388}
3389
3390
d7f3e0c5
GH
3391void
3392typeresolution_info::visit_target_symbol (target_symbol* e)
3393{
cbfbbf69
FCE
3394 // This occurs only if a target symbol was not resolved over in
3395 // tapset.cxx land, that error was properly suppressed, and the
3396 // later unused-expression-elimination pass didn't get rid of it
3397 // either. So we have a target symbol that is believed to be of
3398 // genuine use, yet unresolved by the provider.
ba6f838d
FCE
3399
3400 if (session.verbose > 2)
3401 {
3402 clog << "Resolution problem with ";
3403 if (current_function)
3404 {
3405 clog << "function " << current_function->name << endl;
3406 current_function->body->print (clog);
3407 clog << endl;
3408 }
3409 else if (current_probe)
3410 {
3411 clog << "probe " << current_probe->name << endl;
3412 current_probe->body->print (clog);
3413 clog << endl;
3414 }
57148ee7 3415 else
ba6f838d
FCE
3416 clog << "other" << endl;
3417 }
3418
9b48ce88
FCE
3419 if (e->saved_conversion_error)
3420 throw (* (e->saved_conversion_error));
3421 else
3422 throw semantic_error("unresolved target-symbol expression", e->tok);
d7f3e0c5
GH
3423}
3424
3425
9b5af295
JS
3426void
3427typeresolution_info::visit_cast_op (cast_op* e)
3428{
3429 // Like target_symbol, a cast_op shouldn't survive this far
3430 // unless it was not resolved and its value is really needed.
3431 if (e->saved_conversion_error)
3432 throw (* (e->saved_conversion_error));
3433 else
3434 throw semantic_error("unresolved cast expression", e->tok);
3435}
3436
3437
2b066ec1
FCE
3438void
3439typeresolution_info::visit_arrayindex (arrayindex* e)
3440{
2b066ec1 3441
d02548c0
GH
3442 symbol *array = NULL;
3443 hist_op *hist = NULL;
3444 classify_indexable(e->base, array, hist);
57148ee7 3445
d02548c0
GH
3446 // Every hist_op has type [int]:int, that is to say, every hist_op
3447 // is a pseudo-one-dimensional integer array type indexed by
3448 // integers (bucket numbers).
3449
3450 if (hist)
3451 {
3452 if (e->indexes.size() != 1)
3453 unresolved (e->tok);
3454 t = pe_long;
3455 e->indexes[0]->visit (this);
3456 if (e->indexes[0]->type != pe_long)
3457 unresolved (e->tok);
a4636912 3458 hist->visit (this);
1bbeef03
GH
3459 if (e->type != pe_long)
3460 {
3461 e->type = pe_long;
3462 resolved (e->tok, pe_long);
3463 }
d02548c0
GH
3464 return;
3465 }
3466
3467 // Now we are left with "normal" map inference and index checking.
3468
3469 assert (array);
3470 assert (array->referent != 0);
3471 resolve_2types (e, array->referent, this, t);
2b066ec1
FCE
3472
3473 // now resolve the array indexes
69c68955
FCE
3474
3475 // if (e->referent->index_types.size() == 0)
3476 // // redesignate referent as array
3477 // e->referent->set_arity (e->indexes.size ());
2b066ec1 3478
d02548c0 3479 if (e->indexes.size() != array->referent->index_types.size())
2b066ec1
FCE
3480 unresolved (e->tok); // symbol resolution should prevent this
3481 else for (unsigned i=0; i<e->indexes.size(); i++)
3482 {
3483 expression* ee = e->indexes[i];
d02548c0 3484 exp_type& ft = array->referent->index_types [i];
2b066ec1
FCE
3485 t = ft;
3486 ee->visit (this);
3487 exp_type at = ee->type;
3488
3489 if ((at == pe_string || at == pe_long) && ft == pe_unknown)
3490 {
3491 // propagate to formal type
3492 ft = at;
d02548c0 3493 resolved (array->referent->tok, ft);
2b066ec1
FCE
3494 // uses array decl as there is no token for "formal type"
3495 }
3496 if (at == pe_stats)
3497 invalid (ee->tok, at);
3498 if (ft == pe_stats)
3499 invalid (ee->tok, ft);
3500 if (at != pe_unknown && ft != pe_unknown && ft != at)
3501 mismatch (e->tok, at, ft);
3502 if (at == pe_unknown)
313b2f74 3503 unresolved (ee->tok);
2b066ec1
FCE
3504 }
3505}
3506
3507
3508void
3509typeresolution_info::visit_functioncall (functioncall* e)
3510{
3511 assert (e->referent != 0);
3512
57148ee7 3513 resolve_2types (e, e->referent, this, t, true); // accept unknown type
2b066ec1
FCE
3514
3515 if (e->type == pe_stats)
3516 invalid (e->tok, e->type);
3517
2b066ec1
FCE
3518 // now resolve the function parameters
3519 if (e->args.size() != e->referent->formal_args.size())
3520 unresolved (e->tok); // symbol resolution should prevent this
3521 else for (unsigned i=0; i<e->args.size(); i++)
3522 {
3523 expression* ee = e->args[i];
3524 exp_type& ft = e->referent->formal_args[i]->type;
3525 const token* fe_tok = e->referent->formal_args[i]->tok;
3526 t = ft;
3527 ee->visit (this);
3528 exp_type at = ee->type;
57148ee7 3529
2b066ec1
FCE
3530 if (((at == pe_string) || (at == pe_long)) && ft == pe_unknown)
3531 {
3532 // propagate to formal arg
3533 ft = at;
3534 resolved (e->referent->formal_args[i]->tok, ft);
3535 }
3536 if (at == pe_stats)
3537 invalid (e->tok, at);
3538 if (ft == pe_stats)
3539 invalid (fe_tok, ft);
3540 if (at != pe_unknown && ft != pe_unknown && ft != at)
3541 mismatch (e->tok, at, ft);
3542 if (at == pe_unknown)
3543 unresolved (e->tok);
3544 }
3545}
3546
3547
3548void
3549typeresolution_info::visit_block (block* e)
3550{
3551 for (unsigned i=0; i<e->statements.size(); i++)
3552 {
57148ee7 3553 try
2b066ec1
FCE
3554 {
3555 t = pe_unknown;
3556 e->statements[i]->visit (this);
3557 }
3558 catch (const semantic_error& e)
3559 {
3560 session.print_error (e);
3561 }
3562 }
3563}
3564
3565
54dfabe9 3566void
78f6bba6 3567typeresolution_info::visit_embeddedcode (embeddedcode*)
54dfabe9
FCE
3568{
3569}
3570
3571
2b066ec1
FCE
3572void
3573typeresolution_info::visit_if_statement (if_statement* e)
3574{
3575 t = pe_long;
3576 e->condition->visit (this);
3577
3578 t = pe_unknown;
3579 e->thenblock->visit (this);
3580
3581 if (e->elseblock)
3582 {
3583 t = pe_unknown;
3584 e->elseblock->visit (this);
3585 }
3586}
3587
3588
3589void
3590typeresolution_info::visit_for_loop (for_loop* e)
3591{
3592 t = pe_unknown;
cbfbbf69 3593 if (e->init) e->init->visit (this);
2b066ec1
FCE
3594 t = pe_long;
3595 e->cond->visit (this);
3596 t = pe_unknown;
57148ee7 3597 if (e->incr) e->incr->visit (this);
2b066ec1 3598 t = pe_unknown;
57148ee7 3599 e->block->visit (this);
2b066ec1
FCE
3600}
3601
3602
69c68955
FCE
3603void
3604typeresolution_info::visit_foreach_loop (foreach_loop* e)
3605{
3606 // See also visit_arrayindex.
3607 // This is different in that, being a statement, we can't assign
3608 // a type to the outer array, only propagate to/from the indexes
3609
3610 // if (e->referent->index_types.size() == 0)
3611 // // redesignate referent as array
3612 // e->referent->set_arity (e->indexes.size ());
3613
d02548c0
GH
3614 symbol *array = NULL;
3615 hist_op *hist = NULL;
3616 classify_indexable(e->base, array, hist);
69c68955 3617
d02548c0 3618 if (hist)
57148ee7 3619 {
d02548c0
GH
3620 if (e->indexes.size() != 1)
3621 unresolved (e->tok);
3622 t = pe_long;
3623 e->indexes[0]->visit (this);
3624 if (e->indexes[0]->type != pe_long)
3625 unresolved (e->tok);
a4636912 3626 hist->visit (this);
d02548c0
GH
3627 }
3628 else
3629 {
57148ee7 3630 assert (array);
d02548c0
GH
3631 if (e->indexes.size() != array->referent->index_types.size())
3632 unresolved (e->tok); // symbol resolution should prevent this
3633 else for (unsigned i=0; i<e->indexes.size(); i++)
3634 {
3635 expression* ee = e->indexes[i];
3636 exp_type& ft = array->referent->index_types [i];
3637 t = ft;
3638 ee->visit (this);
3639 exp_type at = ee->type;
57148ee7 3640
d02548c0
GH
3641 if ((at == pe_string || at == pe_long) && ft == pe_unknown)
3642 {
3643 // propagate to formal type
3644 ft = at;
3645 resolved (array->referent->tok, ft);
3646 // uses array decl as there is no token for "formal type"
3647 }
3648 if (at == pe_stats)
3649 invalid (ee->tok, at);
3650 if (ft == pe_stats)
3651 invalid (ee->tok, ft);
3652 if (at != pe_unknown && ft != pe_unknown && ft != at)
3653 mismatch (e->tok, at, ft);
3654 if (at == pe_unknown)
3655 unresolved (ee->tok);
3656 }
69c68955
FCE
3657 }
3658
27f21e8c
DS
3659 if (e->limit)
3660 {
3661 t = pe_long;
3662 e->limit->visit (this);
3663 }
3664
69c68955 3665 t = pe_unknown;
57148ee7 3666 e->block->visit (this);
69c68955
FCE
3667}
3668
3669
2b066ec1 3670void
78f6bba6 3671typeresolution_info::visit_null_statement (null_statement*)
2b066ec1
FCE
3672{
3673}
3674
3675
3676void
3677typeresolution_info::visit_expr_statement (expr_statement* e)
3678{
3679 t = pe_unknown;
3680 e->value->visit (this);
3681}
3682
3683
57148ee7 3684struct delete_statement_typeresolution_info:
d98d459c
GH
3685 public throwing_visitor
3686{
3687 typeresolution_info *parent;
3688 delete_statement_typeresolution_info (typeresolution_info *p):
3689 throwing_visitor ("invalid operand of delete expression"),
3690 parent (p)
3691 {}
3692
3693 void visit_arrayindex (arrayindex* e)
3694 {
3695 parent->visit_arrayindex (e);
3696 }
57148ee7 3697
d98d459c
GH
3698 void visit_symbol (symbol* e)
3699 {
3700 exp_type ignored = pe_unknown;
57148ee7 3701 assert (e->referent != 0);
d98d459c
GH
3702 resolve_2types (e, e->referent, parent, ignored);
3703 }
3704};
3705
3706
2b066ec1
FCE
3707void
3708typeresolution_info::visit_delete_statement (delete_statement* e)
3709{
d98d459c
GH
3710 delete_statement_typeresolution_info di (this);
3711 t = pe_unknown;
3712 e->value->visit (&di);
2b066ec1
FCE
3713}
3714
3715
f3c26ea5 3716void
78f6bba6 3717typeresolution_info::visit_next_statement (next_statement*)
f3c26ea5
FCE
3718{
3719}
3720
3721
3722void
78f6bba6 3723typeresolution_info::visit_break_statement (break_statement*)
f3c26ea5
FCE
3724{
3725}
3726
3727
3728void
78f6bba6 3729typeresolution_info::visit_continue_statement (continue_statement*)
f3c26ea5
FCE
3730{
3731}
3732
3733
2b066ec1
FCE
3734void
3735typeresolution_info::visit_array_in (array_in* e)
3736{
ce10591c
FCE
3737 // all unary operators only work on numerics
3738 exp_type t1 = t;
3739 t = pe_unknown; // array value can be anything
3740 e->operand->visit (this);
3741
3742 if (t1 == pe_unknown && e->type != pe_unknown)
3743 ; // already resolved
3744 else if (t1 == pe_string || t1 == pe_stats)
3745 mismatch (e->tok, t1, pe_long);
3746 else if (e->type == pe_unknown)
3747 {
3748 e->type = pe_long;
3749 resolved (e->tok, e->type);
3750 }
2b066ec1
FCE
3751}
3752
3753
3754void
3755typeresolution_info::visit_return_statement (return_statement* e)
3756{
3757 // This is like symbol, where the referent is
3758 // the return value of the function.
3759
57148ee7 3760 // translation pass will print error
2b066ec1 3761 if (current_function == 0)
22f46231 3762 return;
2b066ec1
FCE
3763
3764 exp_type& e_type = current_function->type;
3765 t = current_function->type;
3766 e->value->visit (this);
3767
3768 if (e_type != pe_unknown && e->value->type != pe_unknown
3769 && e_type != e->value->type)
3770 mismatch (current_function->tok, e_type, e->value->type);
57148ee7 3771 if (e_type == pe_unknown &&
2b066ec1
FCE
3772 (e->value->type == pe_long || e->value->type == pe_string))
3773 {
3774 // propagate non-statistics from value
3775 e_type = e->value->type;
3776 resolved (current_function->tok, e->value->type);
3777 }
3778 if (e->value->type == pe_stats)
3779 invalid (e->value->tok, e->value->type);
3780}
3781
57148ee7 3782void
d02548c0
GH
3783typeresolution_info::visit_print_format (print_format* e)
3784{
3785 size_t unresolved_args = 0;
3786
1bbeef03
GH
3787 if (e->hist)
3788 {
3789 e->hist->visit(this);
3790 }
3791
3792 else if (e->print_with_format)
d02548c0
GH
3793 {
3794 // If there's a format string, we can do both inference *and*
3795 // checking.
3796
3797 // First we extract the subsequence of formatting components
3798 // which are conversions (not just literal string components)
3799
34201621 3800 unsigned expected_num_args = 0;
d02548c0
GH
3801 std::vector<print_format::format_component> components;
3802 for (size_t i = 0; i < e->components.size(); ++i)
3803 {
3804 if (e->components[i].type == print_format::conv_unspecified)
3805 throw semantic_error ("Unspecified conversion in print operator format string",
3806 e->tok);
b5852334 3807 else if (e->components[i].type == print_format::conv_literal)
d02548c0
GH
3808 continue;
3809 components.push_back(e->components[i]);
34201621
DB
3810 ++expected_num_args;
3811 if (e->components[i].widthtype == print_format::width_dynamic)
3812 ++expected_num_args;
3813 if (e->components[i].prectype == print_format::prec_dynamic)
3814 ++expected_num_args;
d02548c0
GH
3815 }
3816
3817 // Then we check that the number of conversions and the number
3818 // of args agree.
3819
34201621 3820 if (expected_num_args != e->args.size())
d02548c0
GH
3821 throw semantic_error ("Wrong number of args to formatted print operator",
3822 e->tok);
3823
3824 // Then we check that the types of the conversions match the types
3825 // of the args.
34201621 3826 unsigned argno = 0;
d02548c0
GH
3827 for (size_t i = 0; i < components.size(); ++i)
3828 {
34201621
DB
3829 // Check the dynamic width, if specified
3830 if (components[i].widthtype == print_format::width_dynamic)
3831 {
3832 check_arg_type (pe_long, e->args[argno]);
3833 ++argno;
3834 }
3835
3836 // Check the dynamic precision, if specified
3837 if (components[i].prectype == print_format::prec_dynamic)
3838 {
3839 check_arg_type (pe_long, e->args[argno]);
3840 ++argno;
3841 }
3842
d02548c0
GH
3843 exp_type wanted = pe_unknown;
3844
3845 switch (components[i].type)
3846 {
d02548c0
GH
3847 case print_format::conv_unspecified:
3848 case print_format::conv_literal:
3849 assert (false);
3850 break;
3851
3852 case print_format::conv_signed_decimal:
3853 case print_format::conv_unsigned_decimal:
3854 case print_format::conv_unsigned_octal:
58cf0567 3855 case print_format::conv_unsigned_ptr:
d02548c0
GH
3856 case print_format::conv_unsigned_uppercase_hex:
3857 case print_format::conv_unsigned_lowercase_hex:
dc0b623a 3858 case print_format::conv_binary:
fecccf83 3859 case print_format::conv_char:
ec03bd4b 3860 case print_format::conv_memory:
30c94a80 3861 case print_format::conv_memory_hex:
d02548c0
GH
3862 wanted = pe_long;
3863 break;
3864
3865 case print_format::conv_string:
3866 wanted = pe_string;
3867 break;
3868 }
3869
3870 assert (wanted != pe_unknown);
34201621
DB
3871 check_arg_type (wanted, e->args[argno]);
3872 ++argno;
d02548c0
GH
3873 }
3874 }
3875 else
3876 {
3877 // Without a format string, the best we can do is require that
3878 // each argument resolve to a concrete type.
3879 for (size_t i = 0; i < e->args.size(); ++i)
3880 {
3881 t = pe_unknown;
3882 e->args[i]->visit (this);
3883 if (e->args[i]->type == pe_unknown)
3884 {
3885 unresolved (e->args[i]->tok);
3886 ++unresolved_args;
3887 }
3888 }
3889 }
57148ee7 3890
d02548c0
GH
3891 if (unresolved_args == 0)
3892 {
3893 if (e->type == pe_unknown)
3894 {
3895 if (e->print_to_stream)
3896 e->type = pe_long;
3897 else
57148ee7 3898 e->type = pe_string;
d02548c0
GH
3899 resolved (e->tok, e->type);
3900 }
3901 }
3902 else
3903 {
3904 e->type = pe_unknown;
3905 unresolved (e->tok);
3906 }
3907}
3908
3909
57148ee7 3910void
d02548c0
GH
3911typeresolution_info::visit_stat_op (stat_op* e)
3912{
3913 t = pe_stats;
3914 e->stat->visit (this);
3915 if (e->type == pe_unknown)
3916 {
3917 e->type = pe_long;
3918 resolved (e->tok, e->type);
3919 }
07c17d67 3920 else if (e->type != pe_long)
d02548c0
GH
3921 mismatch (e->tok, e->type, pe_long);
3922}
3923
57148ee7 3924void
d02548c0
GH
3925typeresolution_info::visit_hist_op (hist_op* e)
3926{
3927 t = pe_stats;
3928 e->stat->visit (this);
3929}
3930
2b066ec1 3931
34201621
DB
3932void
3933typeresolution_info::check_arg_type (exp_type wanted, expression* arg)
3934{
3935 t = wanted;
3936 arg->visit (this);
3937
3938 if (arg->type == pe_unknown)
3939 {
3940 arg->type = wanted;
3941 resolved (arg->tok, wanted);
3942 }
3943 else if (arg->type != wanted)
3944 {
3945 mismatch (arg->tok, arg->type, wanted);
3946 }
3947}
3948
3949
2b066ec1
FCE
3950void
3951typeresolution_info::unresolved (const token* tok)
3952{
3953 num_still_unresolved ++;
3954
3955 if (assert_resolvability)
3956 {
6c543717 3957 stringstream msg;
bdef2583
FCE
3958 string nm = (current_function ? current_function->name :
3959 current_probe ? current_probe->name :
5d23847d 3960 "probe condition");
6c543717
FCE
3961 msg << nm + " with unresolved type";
3962 session.print_error (semantic_error (msg.str(), tok));
2b066ec1
FCE
3963 }
3964}
3965
3966
3967void
3968typeresolution_info::invalid (const token* tok, exp_type pe)
3969{
3970 num_still_unresolved ++;
3971
3972 if (assert_resolvability)
3973 {
6c543717 3974 stringstream msg;
bdef2583
FCE
3975 string nm = (current_function ? current_function->name :
3976 current_probe ? current_probe->name :
5d23847d 3977 "probe condition");
022b623f 3978 if (tok && tok->type == tok_operator)
6c543717 3979 msg << nm + " uses invalid operator";
2b066ec1 3980 else
6c543717
FCE
3981 msg << nm + " with invalid type " << pe;
3982 session.print_error (semantic_error (msg.str(), tok));
2b066ec1
FCE
3983 }
3984}
3985
3986
3987void
3988typeresolution_info::mismatch (const token* tok, exp_type t1, exp_type t2)
3989{
3990 num_still_unresolved ++;
3991
3992 if (assert_resolvability)
3993 {
6c543717 3994 stringstream msg;
bdef2583
FCE
3995 string nm = (current_function ? current_function->name :
3996 current_probe ? current_probe->name :
5d23847d 3997 "probe condition");
6c543717
FCE
3998 msg << nm + " with type mismatch (" << t1 << " vs. " << t2 << ")";
3999 session.print_error (semantic_error (msg.str(), tok));
2b066ec1
FCE
4000 }
4001}
4002
4003
4004void
78f6bba6 4005typeresolution_info::resolved (const token*, exp_type)
2b066ec1
FCE
4006{
4007 num_newly_resolved ++;
2b066ec1
FCE
4008}
4009
73267b89 4010/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.515736 seconds and 5 git commands to generate.