sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 3 | /*--- begin ir_match.c ---*/ |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 4 | /*---------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 9 | |
sewardj | 89ae847 | 2013-10-18 14:12:58 +0000 | [diff] [blame] | 10 | Copyright (C) 2004-2013 OpenWorks LLP |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 11 | info@open-works.net |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 12 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 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. |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 17 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 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 |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 26 | 02110-1301, USA. |
| 27 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 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. |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | /* Provides a facility for doing IR tree matching. */ |
| 37 | |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 38 | #include "main_util.h" |
| 39 | #include "ir_match.h" |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /* Assign a value to a binder. Checks for obvious stupidities. */ |
| 43 | |
| 44 | static |
| 45 | void setBindee ( MatchInfo* mi, Int n, 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, IRExpr* p/*attern*/, IRExpr* e/*xpr*/ ) |
| 61 | { |
| 62 | switch (p->tag) { |
| 63 | case Iex_Binder: /* aha, what we were looking for. */ |
| 64 | setBindee(mi, p->Iex.Binder.binder, e); |
| 65 | return True; |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 66 | case Iex_Unop: |
| 67 | if (e->tag != Iex_Unop) return False; |
| 68 | if (p->Iex.Unop.op != e->Iex.Unop.op) return False; |
| 69 | if (!matchWrk(mi, p->Iex.Unop.arg, e->Iex.Unop.arg)) |
| 70 | return False; |
| 71 | return True; |
| 72 | case Iex_Binop: |
| 73 | if (e->tag != Iex_Binop) return False; |
| 74 | if (p->Iex.Binop.op != e->Iex.Binop.op) return False; |
cerion | b85e8bb | 2005-02-16 08:54:33 +0000 | [diff] [blame] | 75 | if (!matchWrk(mi, p->Iex.Binop.arg1, e->Iex.Binop.arg1)) |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 76 | return False; |
cerion | b85e8bb | 2005-02-16 08:54:33 +0000 | [diff] [blame] | 77 | if (!matchWrk(mi, p->Iex.Binop.arg2, e->Iex.Binop.arg2)) |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 78 | return False; |
| 79 | return True; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 80 | case Iex_Load: |
| 81 | if (e->tag != Iex_Load) return False; |
| 82 | if (p->Iex.Load.end != e->Iex.Load.end) return False; |
| 83 | if (p->Iex.Load.ty != e->Iex.Load.ty) return False; |
| 84 | if (!matchWrk(mi, p->Iex.Load.addr, e->Iex.Load.addr)) |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 85 | return False; |
| 86 | return True; |
| 87 | case Iex_Const: |
| 88 | if (e->tag != Iex_Const) return False; |
| 89 | return eqIRConst(p->Iex.Const.con, e->Iex.Const.con); |
| 90 | default: |
| 91 | ppIRExpr(p); |
| 92 | vpanic("match"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* Top level entry point to the matcher. */ |
| 98 | |
| 99 | Bool matchIRExpr ( MatchInfo* mi, IRExpr* p/*attern*/, IRExpr* e/*xpr*/ ) |
| 100 | { |
| 101 | Int i; |
| 102 | for (i = 0; i < N_IRMATCH_BINDERS; i++) |
| 103 | mi->bindee[i] = NULL; |
| 104 | return matchWrk(mi, p, e); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | /*---------------------------------------------------------------*/ |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 110 | /*--- end ir_match.c ---*/ |
sewardj | 69933ac | 2004-12-20 04:12:14 +0000 | [diff] [blame] | 111 | /*---------------------------------------------------------------*/ |