Home | History | Annotate | Download | only in priv
      1 
      2 /*---------------------------------------------------------------*/
      3 /*--- begin                                        ir_match.c ---*/
      4 /*---------------------------------------------------------------*/
      5 
      6 /*
      7    This file is part of Valgrind, a dynamic binary instrumentation
      8    framework.
      9 
     10    Copyright (C) 2004-2017 OpenWorks LLP
     11       info (at) open-works.net
     12 
     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.
     17 
     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.
     22 
     23    You should have received a copy of the GNU General Public License
     24    along with this program; if not, write to the Free Software
     25    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     26    02110-1301, USA.
     27 
     28    The GNU General Public License is contained in the file COPYING.
     29 
     30    Neither the names of the U.S. Department of Energy nor the
     31    University of California nor the names of its contributors may be
     32    used to endorse or promote products derived from this software
     33    without prior written permission.
     34 */
     35 
     36 /* Provides a facility for doing IR tree matching. */
     37 
     38 #include "main_util.h"
     39 #include "ir_match.h"
     40 
     41 
     42 /* Assign a value to a binder.  Checks for obvious stupidities. */
     43 
     44 static
     45 void setBindee ( MatchInfo* mi, Int n, const IRExpr* bindee )
     46 {
     47    if (n < 0 || n >= N_IRMATCH_BINDERS)
     48       vpanic("setBindee: out of range index");
     49    if (mi->bindee[n] != NULL)
     50       vpanic("setBindee: bindee already set");
     51    mi->bindee[n] = bindee;
     52 }
     53 
     54 
     55 /* This is the actual matching function, recursing over the pattern
     56    and expression trees in the obvious way, and dumping any matches
     57    found into 'mi'. */
     58 
     59 static
     60 Bool matchWrk ( MatchInfo* mi, const IRExpr* p/*attern*/,
     61                 const IRExpr* e/*xpr*/ )
     62 {
     63    switch (p->tag) {
     64       case Iex_Binder: /* aha, what we were looking for. */
     65          setBindee(mi, p->Iex.Binder.binder, e);
     66          return True;
     67       case Iex_Unop:
     68          if (e->tag != Iex_Unop) return False;
     69          if (p->Iex.Unop.op != e->Iex.Unop.op) return False;
     70          if (!matchWrk(mi, p->Iex.Unop.arg, e->Iex.Unop.arg))
     71             return False;
     72          return True;
     73       case Iex_Binop:
     74          if (e->tag != Iex_Binop) return False;
     75          if (p->Iex.Binop.op != e->Iex.Binop.op) return False;
     76          if (!matchWrk(mi, p->Iex.Binop.arg1, e->Iex.Binop.arg1))
     77             return False;
     78          if (!matchWrk(mi, p->Iex.Binop.arg2, e->Iex.Binop.arg2))
     79             return False;
     80          return True;
     81       case Iex_Load:
     82          if (e->tag != Iex_Load) return False;
     83          if (p->Iex.Load.end != e->Iex.Load.end) return False;
     84          if (p->Iex.Load.ty != e->Iex.Load.ty) return False;
     85          if (!matchWrk(mi, p->Iex.Load.addr, e->Iex.Load.addr))
     86             return False;
     87          return True;
     88       case Iex_Const:
     89          if (e->tag != Iex_Const) return False;
     90          return eqIRConst(p->Iex.Const.con, e->Iex.Const.con);
     91       default:
     92          ppIRExpr(p);
     93          vpanic("match");
     94    }
     95 }
     96 
     97 
     98 /* Top level entry point to the matcher. */
     99 
    100 Bool matchIRExpr ( MatchInfo* mi, const IRExpr* p/*attern*/,
    101                    const IRExpr* e/*xpr*/ )
    102 {
    103    Int i;
    104    for (i = 0; i < N_IRMATCH_BINDERS; i++)
    105       mi->bindee[i] = NULL;
    106    return matchWrk(mi, p, e);
    107 }
    108 
    109 
    110 
    111 /*---------------------------------------------------------------*/
    112 /*--- end                                          ir_match.c ---*/
    113 /*---------------------------------------------------------------*/
    114