sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The JITter proper: register allocation & code improvement ---*/ |
njn | 3cbfbc1 | 2005-05-13 23:11:40 +0000 | [diff] [blame] | 4 | /*--- m_translate.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2005 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 32 | #include "core.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 33 | #include "pub_core_aspacemgr.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 34 | #include "pub_core_libcbase.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 35 | #include "pub_core_libcprint.h" |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 36 | #include "pub_core_main.h" // for VG_(bbs_done) |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 37 | #include "pub_core_options.h" |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 38 | #include "pub_core_profile.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 39 | #include "pub_core_redir.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 40 | #include "pub_core_signals.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 41 | #include "pub_core_tooliface.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 42 | #include "pub_core_translate.h" |
| 43 | #include "pub_core_transtab.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 45 | |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 46 | /*------------------------------------------------------------*/ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 47 | /*--- %SP-update pass ---*/ |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 48 | /*------------------------------------------------------------*/ |
| 49 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 50 | /* NOTE: this comment is out of date */ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 51 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 52 | /* For tools that want to know about %ESP changes, this pass adds |
| 53 | in the appropriate hooks. We have to do it after the tool's |
| 54 | instrumentation, so the tool doesn't have to worry about the CCALLs |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 55 | it adds in, and we must do it before register allocation because |
| 56 | spilled temps make it much harder to work out the %esp deltas. |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 57 | Thus we have it as an extra phase between the two. |
| 58 | |
| 59 | We look for "GETL %ESP, t_ESP", then track ADDs and SUBs of |
| 60 | literal values to t_ESP, and the total delta of the ADDs/SUBs. Then if |
| 61 | "PUTL t_ESP, %ESP" happens, we call the helper with the known delta. We |
| 62 | also cope with "MOVL t_ESP, tX", making tX the new t_ESP. If any other |
| 63 | instruction clobbers t_ESP, we don't track it anymore, and fall back to |
| 64 | the delta-is-unknown case. That case is also used when the delta is not |
| 65 | a nice small amount, or an unknown amount. |
| 66 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | |
| 68 | static |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 69 | IRBB* vg_SP_update_pass ( IRBB* bb_in, VexGuestLayout* layout, |
njn | 8786e53 | 2005-03-24 04:35:11 +0000 | [diff] [blame] | 70 | IRType gWordTy, IRType hWordTy ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 72 | Int i, j, minoff_ST, maxoff_ST, sizeof_SP, offset_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 73 | IRDirty *dcall, *d; |
| 74 | IRStmt* st; |
| 75 | IRExpr* e; |
| 76 | IRArray* descr; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 77 | IRTemp curr; |
| 78 | IRType typeof_SP; |
| 79 | Long delta; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 81 | /* Set up BB */ |
| 82 | IRBB* bb = emptyIRBB(); |
| 83 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 84 | bb->next = dopyIRExpr(bb_in->next); |
| 85 | bb->jumpkind = bb_in->jumpkind; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 86 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 87 | curr = IRTemp_INVALID; |
| 88 | delta = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 89 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 90 | sizeof_SP = layout->sizeof_SP; |
| 91 | offset_SP = layout->offset_SP; |
| 92 | typeof_SP = sizeof_SP==4 ? Ity_I32 : Ity_I64; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 93 | vg_assert(sizeof_SP == 4 || sizeof_SP == 8); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 94 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 95 | # define IS_ADD(op) (sizeof_SP==4 ? ((op)==Iop_Add32) : ((op)==Iop_Add64)) |
| 96 | # define IS_SUB(op) (sizeof_SP==4 ? ((op)==Iop_Sub32) : ((op)==Iop_Sub64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 97 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 98 | # define IS_ADD_OR_SUB(op) (IS_ADD(op) || IS_SUB(op)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 99 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 100 | # define GET_CONST(con) \ |
| 101 | (sizeof_SP==4 ? (Long)(Int)(con->Ico.U32) \ |
| 102 | : (Long)(con->Ico.U64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 103 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 104 | # define DO(kind, syze) \ |
| 105 | do { \ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 106 | if (!VG_(tdict).track_##kind##_mem_stack_##syze) \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 107 | goto generic; \ |
| 108 | \ |
| 109 | /* I don't know if it's really necessary to say that the */ \ |
| 110 | /* call reads the stack pointer. But anyway, we do. */ \ |
| 111 | dcall = unsafeIRDirty_0_N( \ |
| 112 | 1/*regparms*/, \ |
| 113 | "track_" #kind "_mem_stack_" #syze, \ |
njn | cf81d55 | 2005-03-31 04:52:26 +0000 | [diff] [blame] | 114 | VG_(tdict).track_##kind##_mem_stack_##syze, \ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 115 | mkIRExprVec_1(IRExpr_Tmp(curr)) \ |
| 116 | ); \ |
| 117 | dcall->nFxState = 1; \ |
| 118 | dcall->fxState[0].fx = Ifx_Read; \ |
| 119 | dcall->fxState[0].offset = layout->offset_SP; \ |
| 120 | dcall->fxState[0].size = layout->sizeof_SP; \ |
| 121 | \ |
| 122 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); \ |
| 123 | } while (0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 124 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 125 | for (i = 0; i < bb_in->stmts_used; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 126 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 127 | st = bb_in->stmts[i]; |
| 128 | if (!st) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 129 | continue; |
| 130 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 131 | /* t = Get(sp): curr = t, delta = 0 */ |
| 132 | if (st->tag != Ist_Tmp) goto case2; |
| 133 | e = st->Ist.Tmp.data; |
| 134 | if (e->tag != Iex_Get) goto case2; |
| 135 | if (e->Iex.Get.offset != offset_SP) goto case2; |
| 136 | if (e->Iex.Get.ty != typeof_SP) goto case2; |
| 137 | curr = st->Ist.Tmp.tmp; |
| 138 | delta = 0; |
| 139 | addStmtToIRBB( bb, st ); |
| 140 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 141 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 142 | case2: |
| 143 | /* t' = curr +/- const: curr = t', delta +=/-= const */ |
| 144 | if (st->tag != Ist_Tmp) goto case3; |
| 145 | e = st->Ist.Tmp.data; |
| 146 | if (e->tag != Iex_Binop) goto case3; |
| 147 | if (e->Iex.Binop.arg1->tag != Iex_Tmp) goto case3; |
| 148 | if (e->Iex.Binop.arg1->Iex.Tmp.tmp != curr) goto case3; |
| 149 | if (e->Iex.Binop.arg2->tag != Iex_Const) goto case3; |
| 150 | if (!IS_ADD_OR_SUB(e->Iex.Binop.op)) goto case3; |
| 151 | curr = st->Ist.Tmp.tmp; |
| 152 | if (IS_ADD(e->Iex.Binop.op)) |
| 153 | delta += GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 154 | else |
| 155 | delta -= GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 156 | addStmtToIRBB( bb, st ); |
| 157 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 158 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 159 | case3: |
| 160 | /* t' = curr: curr = t' */ |
| 161 | if (st->tag != Ist_Tmp) goto case4; |
| 162 | e = st->Ist.Tmp.data; |
| 163 | if (e->tag != Iex_Tmp) goto case4; |
| 164 | if (e->Iex.Tmp.tmp != curr) goto case4; |
| 165 | curr = st->Ist.Tmp.tmp; |
| 166 | addStmtToIRBB( bb, st ); |
| 167 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 168 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 169 | case4: |
| 170 | /* Put(sp) = curr */ |
| 171 | if (st->tag != Ist_Put) goto case5; |
| 172 | if (st->Ist.Put.offset != offset_SP) goto case5; |
| 173 | if (st->Ist.Put.data->tag != Iex_Tmp) goto case5; |
| 174 | if (st->Ist.Put.data->Iex.Tmp.tmp == curr) { |
| 175 | switch (delta) { |
| 176 | case 0: addStmtToIRBB(bb,st); delta = 0; continue; |
| 177 | case 4: DO(die, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 178 | case -4: DO(new, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 179 | case 8: DO(die, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 180 | case -8: DO(new, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 181 | case 12: DO(die, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 182 | case -12: DO(new, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 183 | case 16: DO(die, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 184 | case -16: DO(new, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 185 | case 32: DO(die, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 186 | case -32: DO(new, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 187 | default: goto generic; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 188 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 189 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 190 | IRTemp old_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 191 | generic: |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 192 | /* Pass both the old and new SP values to this helper. */ |
| 193 | old_SP = newIRTemp(bb->tyenv, typeof_SP); |
| 194 | addStmtToIRBB( |
| 195 | bb, |
| 196 | IRStmt_Tmp( old_SP, IRExpr_Get(offset_SP, typeof_SP) ) |
| 197 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 198 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 199 | dcall = unsafeIRDirty_0_N( |
| 200 | 2/*regparms*/, |
| 201 | "VG_(unknown_SP_update)", &VG_(unknown_SP_update), |
| 202 | mkIRExprVec_2( IRExpr_Tmp(old_SP), st->Ist.Put.data ) |
| 203 | ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 204 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 205 | |
| 206 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 207 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 208 | curr = st->Ist.Put.data->Iex.Tmp.tmp; |
| 209 | delta = 0; |
| 210 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 211 | } |
| 212 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 213 | case5: |
| 214 | /* PutI or Dirty call which overlaps SP: complain. We can't |
| 215 | deal with SP changing in weird ways (well, we can, but not at |
| 216 | this time of night). */ |
| 217 | if (st->tag == Ist_PutI) { |
| 218 | descr = st->Ist.PutI.descr; |
| 219 | minoff_ST = descr->base; |
| 220 | maxoff_ST = descr->base + descr->nElems * sizeofIRType(descr->elemTy) - 1; |
| 221 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 222 | goto complain; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 223 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 224 | if (st->tag == Ist_Dirty) { |
| 225 | d = st->Ist.Dirty.details; |
| 226 | for (j = 0; j < d->nFxState; j++) { |
| 227 | minoff_ST = d->fxState[j].offset; |
| 228 | maxoff_ST = d->fxState[j].offset + d->fxState[j].size - 1; |
| 229 | if (d->fxState[j].fx == Ifx_Read || d->fxState[j].fx == Ifx_None) |
| 230 | continue; |
| 231 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 232 | goto complain; |
| 233 | } |
| 234 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 235 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 236 | /* well, not interesting. Just copy and keep going. */ |
| 237 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 238 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 239 | } /* for (i = 0; i < bb_in->stmts_used; i++) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 240 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 241 | return bb; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 242 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 243 | complain: |
| 244 | VG_(core_panic)("vg_SP_update_pass: PutI or Dirty which overlaps SP"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 245 | |
| 246 | } |
sewardj | 7c4b604 | 2003-06-14 15:47:15 +0000 | [diff] [blame] | 247 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 248 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 249 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 250 | #if 0 |
| 251 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 252 | st = bb_in->stmts[i]; |
| 253 | if (!st) |
| 254 | continue; |
| 255 | if (st->tag != Ist_Put) |
| 256 | goto boring; |
| 257 | offP = st->Ist.Put.offset; |
| 258 | if (offP != layout->offset_SP) |
| 259 | goto boring; |
| 260 | szP = sizeofIRType(typeOfIRExpr(bb_in->tyenv, st->Ist.Put.data)); |
| 261 | if (szP != layout->sizeof_SP) |
| 262 | goto boring; |
| 263 | vg_assert(isAtom(st->Ist.Put.data)); |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 264 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 265 | /* I don't know if it's really necessary to say that the call reads |
| 266 | the stack pointer. But anyway, we do. */ |
| 267 | dcall = unsafeIRDirty_0_N( |
| 268 | mkIRCallee(1, "VG_(unknown_esp_update)", |
| 269 | (HWord)&VG_(unknown_esp_update)), |
| 270 | mkIRExprVec_1(st->Ist.Put.data) |
| 271 | ); |
| 272 | dcall->nFxState = 1; |
| 273 | dcall->fxState[0].fx = Ifx_Read; |
| 274 | dcall->fxState[0].offset = layout->offset_SP; |
| 275 | dcall->fxState[0].size = layout->sizeof_SP; |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 276 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 277 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
| 278 | |
| 279 | boring: |
| 280 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 281 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 282 | #endif |
| 283 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 284 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 285 | /*------------------------------------------------------------*/ |
| 286 | /*--- Main entry point for the JITter. ---*/ |
| 287 | /*------------------------------------------------------------*/ |
| 288 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 289 | /* Vex dumps the final code in here. Then we can copy it off |
| 290 | wherever we like. */ |
sewardj | fb154a5 | 2005-03-31 15:48:57 +0000 | [diff] [blame] | 291 | #define N_TMPBUF 20000 |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 292 | static UChar tmpbuf[N_TMPBUF]; |
| 293 | |
| 294 | /* Function pointers we must supply to LibVEX in order that it |
| 295 | can bomb out and emit messages under Valgrind's control. */ |
| 296 | __attribute__ ((noreturn)) |
| 297 | static |
| 298 | void failure_exit ( void ) |
| 299 | { |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 300 | LibVEX_ShowAllocStats(); |
sewardj | c621f76 | 2004-11-26 13:49:59 +0000 | [diff] [blame] | 301 | VG_(core_panic)("LibVEX called failure_exit()."); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 305 | void log_bytes ( HChar* bytes, Int nbytes ) |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 306 | { |
| 307 | Int i; |
| 308 | for (i = 0; i < nbytes-3; i += 4) |
| 309 | VG_(printf)("%c%c%c%c", bytes[i], bytes[i+1], bytes[i+2], bytes[i+3]); |
| 310 | for (; i < nbytes; i++) |
| 311 | VG_(printf)("%c", bytes[i]); |
| 312 | } |
| 313 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 314 | /* Translate the basic block beginning at orig_addr, and add it to |
| 315 | the translation cache & translation table. Unless 'debugging' is true, |
| 316 | in which case the call is being done for debugging purposes, so |
| 317 | (a) throw away the translation once it is made, and (b) produce a |
| 318 | load of debugging output. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 319 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 320 | 'tid' is the identity of the thread needing this block. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 321 | */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 322 | |
| 323 | /* This stops Vex from chasing into function entry points that we wish |
| 324 | to redirect. Chasing across them obviously defeats the redirect |
| 325 | mechanism, with bad effects for Memcheck, Addrcheck, and possibly |
| 326 | others. */ |
| 327 | static Bool chase_into_ok ( Addr64 addr64 ) |
| 328 | { |
| 329 | Addr addr = (Addr)addr64; |
| 330 | if (addr != VG_(code_redirect)(addr)) { |
| 331 | if (0) VG_(printf)("not chasing into 0x%x\n", addr); |
| 332 | return False; |
| 333 | } else { |
| 334 | return True; /* ok to chase into 'addr' */ |
| 335 | } |
| 336 | } |
| 337 | |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 338 | static Bool need_to_handle_SP_assignment(void) |
| 339 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 340 | return ( VG_(tdict).track_new_mem_stack_4 || |
| 341 | VG_(tdict).track_die_mem_stack_4 || |
| 342 | VG_(tdict).track_new_mem_stack_8 || |
| 343 | VG_(tdict).track_die_mem_stack_8 || |
| 344 | VG_(tdict).track_new_mem_stack_12 || |
| 345 | VG_(tdict).track_die_mem_stack_12 || |
| 346 | VG_(tdict).track_new_mem_stack_16 || |
| 347 | VG_(tdict).track_die_mem_stack_16 || |
| 348 | VG_(tdict).track_new_mem_stack_32 || |
| 349 | VG_(tdict).track_die_mem_stack_32 || |
| 350 | VG_(tdict).track_new_mem_stack || |
| 351 | VG_(tdict).track_die_mem_stack ); |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 352 | } |
| 353 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 354 | Bool VG_(translate) ( ThreadId tid, |
| 355 | Addr64 orig_addr, |
| 356 | Bool debugging_translation, |
| 357 | Int debugging_verbosity ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 358 | { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 359 | Addr64 redir, orig_addr0 = orig_addr; |
| 360 | Int tmpbuf_used, verbosity; |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 361 | Bool notrace_until_done; |
| 362 | UInt notrace_until_limit = 0; |
| 363 | Segment* seg; |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 364 | VexGuestExtents vge; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 365 | |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 366 | /* Indicates what arch and subarch we are running on. */ |
| 367 | static VexArch vex_arch = VexArch_INVALID; |
| 368 | static VexSubArch vex_subarch = VexSubArch_INVALID; |
| 369 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 370 | /* Make sure Vex is initialised right. */ |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 371 | VexTranslateResult tres; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 372 | static Bool vex_init_done = False; |
| 373 | |
| 374 | if (!vex_init_done) { |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 375 | Bool ok = VGA_(getArchAndSubArch)( &vex_arch, &vex_subarch ); |
| 376 | if (!ok) { |
| 377 | VG_(printf)("\n"); |
| 378 | VG_(printf)("valgrind: fatal error: unsupported CPU.\n"); |
| 379 | VG_(printf)(" Supported CPUs are:\n"); |
| 380 | VG_(printf)(" * x86 with SSE state (Pentium II or above, " |
| 381 | "AMD Athlon or above)\n"); |
| 382 | VG_(printf)("\n"); |
| 383 | VG_(exit)(1); |
| 384 | } |
| 385 | if (VG_(clo_verbosity) > 2) { |
| 386 | VG_(message)(Vg_DebugMsg, |
| 387 | "Host CPU: arch = %s, subarch = %s", |
| 388 | LibVEX_ppVexArch ( vex_arch ), |
| 389 | LibVEX_ppVexSubArch( vex_subarch ) ); |
| 390 | } |
| 391 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 392 | LibVEX_Init ( &failure_exit, &log_bytes, |
| 393 | 1, /* debug_paranoia */ |
| 394 | False, /* valgrind support */ |
| 395 | &VG_(clo_vex_control) ); |
| 396 | vex_init_done = True; |
| 397 | } |
| 398 | |
| 399 | /* profiling ... */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 400 | VGP_PUSHCC(VgpTranslate); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 401 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 402 | /* Look in the code redirect table to see if we should |
| 403 | translate an alternative address for orig_addr. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 404 | redir = VG_(code_redirect)(orig_addr); |
| 405 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 406 | if (redir != orig_addr && VG_(clo_verbosity) >= 2) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 407 | Bool ok; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 408 | Char name1[64] = ""; |
| 409 | Char name2[64] = ""; |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 410 | name1[0] = name2[0] = 0; |
| 411 | ok = VG_(get_fnname_w_offset)(orig_addr, name1, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 412 | if (!ok) VG_(strcpy)(name1, "???"); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 413 | ok = VG_(get_fnname_w_offset)(redir, name2, 64); |
njn | be73f43 | 2005-03-26 21:34:45 +0000 | [diff] [blame] | 414 | if (!ok) VG_(strcpy)(name2, "???"); |
njn | 3f04d24 | 2005-03-20 18:21:14 +0000 | [diff] [blame] | 415 | VG_(message)(Vg_DebugMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 416 | "REDIR: 0x%llx (%s) redirected to 0x%llx (%s)", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 417 | orig_addr, name1, |
| 418 | redir, name2 ); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 419 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 420 | orig_addr = redir; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 421 | |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 422 | /* If codegen tracing, don't start tracing until |
| 423 | notrace_until_limit blocks have gone by. This avoids printing |
| 424 | huge amounts of useless junk when all we want to see is the last |
| 425 | few blocks translated prior to a failure. Set |
| 426 | notrace_until_limit to be the number of translations to be made |
| 427 | before --trace-codegen= style printing takes effect. */ |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 428 | notrace_until_done |
| 429 | = VG_(get_bbs_translated)() >= notrace_until_limit; |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 430 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 431 | seg = VG_(find_segment)(orig_addr); |
| 432 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 433 | if (!debugging_translation) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 434 | VG_TRACK( pre_mem_read, Vg_CoreTranslate, tid, "", orig_addr, 1 ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 435 | |
sewardj | 85ac6d4 | 2005-02-23 11:36:56 +0000 | [diff] [blame] | 436 | /* If doing any code printing, print a basic block start marker */ |
| 437 | if (VG_(clo_trace_flags) || debugging_translation) { |
| 438 | Char fnname[64] = ""; |
| 439 | VG_(get_fnname_w_offset)(orig_addr, fnname, 64); |
| 440 | VG_(printf)( |
| 441 | "==== BB %d %s(0x%llx) approx BBs exec'd %lld ====\n", |
| 442 | VG_(get_bbs_translated)(), fnname, orig_addr, |
| 443 | VG_(bbs_done)); |
| 444 | } |
| 445 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 446 | if (seg == NULL || |
| 447 | !VG_(seg_contains)(seg, orig_addr, 1) || |
| 448 | (seg->prot & (VKI_PROT_READ|VKI_PROT_EXEC)) == 0) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 449 | /* Code address is bad - deliver a signal instead */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 450 | vg_assert(!VG_(is_addressable)(orig_addr, 1, |
| 451 | VKI_PROT_READ|VKI_PROT_EXEC)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 452 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 453 | if (seg != NULL && VG_(seg_contains)(seg, orig_addr, 1)) { |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 454 | vg_assert((seg->prot & VKI_PROT_EXEC) == 0); |
| 455 | VG_(synth_fault_perms)(tid, orig_addr); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 456 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 457 | VG_(synth_fault_mapping)(tid, orig_addr); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 458 | |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 459 | return False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 460 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 461 | seg->flags |= SF_CODE; /* contains cached code */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 462 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 463 | /* True if a debug trans., or if bit N set in VG_(clo_trace_codegen). */ |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 464 | verbosity = 0; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 465 | if (debugging_translation) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 466 | verbosity = debugging_verbosity; |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 467 | } |
| 468 | else |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 469 | if ( (VG_(clo_trace_flags) > 0 |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 470 | && VG_(get_bbs_translated)() >= VG_(clo_trace_notbelow) )) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 471 | verbosity = VG_(clo_trace_flags); |
sewardj | 167d4e3 | 2004-12-31 01:14:04 +0000 | [diff] [blame] | 472 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 473 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 474 | VGP_PUSHCC(VgpVexTime); |
| 475 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 476 | /* Actually do the translation. */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 477 | tl_assert2(VG_(tdict).tool_instrument, |
| 478 | "you forgot to set VgToolInterface function 'tool_instrument'"); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 479 | tres = LibVEX_Translate ( |
njn | 36932cb | 2005-05-11 22:45:48 +0000 | [diff] [blame] | 480 | vex_arch, vex_subarch, |
| 481 | vex_arch, vex_subarch, |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 482 | (UChar*)ULong_to_Ptr(orig_addr), |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 483 | (Addr64)orig_addr, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 484 | chase_into_ok, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 485 | &vge, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 486 | tmpbuf, N_TMPBUF, &tmpbuf_used, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 487 | VG_(tdict).tool_instrument, |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 488 | need_to_handle_SP_assignment() |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 489 | ? vg_SP_update_pass |
| 490 | : NULL, |
| 491 | True, /* cleanup after instrumentation */ |
| 492 | NULL, |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 493 | verbosity |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 494 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 495 | |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 496 | vg_assert(tres == VexTransOK); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 497 | vg_assert(tmpbuf_used <= N_TMPBUF); |
| 498 | vg_assert(tmpbuf_used > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 499 | |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 500 | VGP_POPCC(VgpVexTime); |
| 501 | |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 502 | #undef DECIDE_IF_PRINTING_CODEGEN |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 503 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 504 | /* Copy data at trans_addr into the translation cache. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 505 | vg_assert(tmpbuf_used > 0 && tmpbuf_used < 65536); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 506 | |
| 507 | // If debugging, don't do anything with the translated block; we |
| 508 | // only did this for the debugging output produced along the way. |
| 509 | if (!debugging_translation) { |
| 510 | // Note that we use orig_addr0, not orig_addr, which might have been |
| 511 | // changed by the redirection |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 512 | VG_(add_to_transtab)( &vge, |
| 513 | orig_addr0, |
| 514 | (Addr)(&tmpbuf[0]), |
| 515 | tmpbuf_used ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 516 | } |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 517 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 518 | VGP_POPCC(VgpTranslate); |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 519 | |
| 520 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /*--------------------------------------------------------------------*/ |
njn | 3cbfbc1 | 2005-05-13 23:11:40 +0000 | [diff] [blame] | 524 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 525 | /*--------------------------------------------------------------------*/ |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 526 | |