Bug 497723 - forgot to restore callgrind output cleanup
[valgrind.git] / VEX / priv / ir_match.c
blob3f4fe8cc54a0b6c6c781beb4e2b6affcea0eec41
2 /*---------------------------------------------------------------*/
3 /*--- begin ir_match.c ---*/
4 /*---------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2004-2017 OpenWorks LLP
11 info@open-works.net
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
28 Neither the names of the U.S. Department of Energy nor the
29 University of California nor the names of its contributors may be
30 used to endorse or promote products derived from this software
31 without prior written permission.
34 /* Provides a facility for doing IR tree matching. */
36 #include "main_util.h"
37 #include "ir_match.h"
40 /* Assign a value to a binder. Checks for obvious stupidities. */
42 static
43 void setBindee ( MatchInfo* mi, Int n, const IRExpr* bindee )
45 if (n < 0 || n >= N_IRMATCH_BINDERS)
46 vpanic("setBindee: out of range index");
47 if (mi->bindee[n] != NULL)
48 vpanic("setBindee: bindee already set");
49 mi->bindee[n] = bindee;
53 /* This is the actual matching function, recursing over the pattern
54 and expression trees in the obvious way, and dumping any matches
55 found into 'mi'. */
57 static
58 Bool matchWrk ( MatchInfo* mi, const IRExpr* p/*attern*/,
59 const IRExpr* e/*xpr*/ )
61 switch (p->tag) {
62 case Iex_Binder: /* aha, what we were looking for. */
63 setBindee(mi, p->Iex.Binder.binder, e);
64 return True;
65 case Iex_Unop:
66 if (e->tag != Iex_Unop) return False;
67 if (p->Iex.Unop.op != e->Iex.Unop.op) return False;
68 if (!matchWrk(mi, p->Iex.Unop.arg, e->Iex.Unop.arg))
69 return False;
70 return True;
71 case Iex_Binop:
72 if (e->tag != Iex_Binop) return False;
73 if (p->Iex.Binop.op != e->Iex.Binop.op) return False;
74 if (!matchWrk(mi, p->Iex.Binop.arg1, e->Iex.Binop.arg1))
75 return False;
76 if (!matchWrk(mi, p->Iex.Binop.arg2, e->Iex.Binop.arg2))
77 return False;
78 return True;
79 case Iex_Load:
80 if (e->tag != Iex_Load) return False;
81 if (p->Iex.Load.end != e->Iex.Load.end) return False;
82 if (p->Iex.Load.ty != e->Iex.Load.ty) return False;
83 if (!matchWrk(mi, p->Iex.Load.addr, e->Iex.Load.addr))
84 return False;
85 return True;
86 case Iex_Const:
87 if (e->tag != Iex_Const) return False;
88 return eqIRConst(p->Iex.Const.con, e->Iex.Const.con);
89 default:
90 ppIRExpr(p);
91 vpanic("match");
96 /* Top level entry point to the matcher. */
98 Bool matchIRExpr ( MatchInfo* mi, const IRExpr* p/*attern*/,
99 const IRExpr* e/*xpr*/ )
101 Int i;
102 for (i = 0; i < N_IRMATCH_BINDERS; i++)
103 mi->bindee[i] = NULL;
104 return matchWrk(mi, p, e);
109 /*---------------------------------------------------------------*/
110 /*--- end ir_match.c ---*/
111 /*---------------------------------------------------------------*/