sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The JITter proper: register allocation & code improvement ---*/ |
| 4 | /*--- vg_translate.c ---*/ |
| 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 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2004 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 | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 33 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 35 | /*------------------------------------------------------------*/ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 36 | /*--- %SP-update pass ---*/ |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 37 | /*------------------------------------------------------------*/ |
| 38 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 39 | /* NOTE: this comment is out of date */ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 40 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 41 | /* For tools that want to know about %ESP changes, this pass adds |
| 42 | in the appropriate hooks. We have to do it after the tool's |
| 43 | instrumentation, so the tool doesn't have to worry about the CCALLs |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 44 | it adds in, and we must do it before register allocation because |
| 45 | spilled temps make it much harder to work out the %esp deltas. |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 46 | Thus we have it as an extra phase between the two. |
| 47 | |
| 48 | We look for "GETL %ESP, t_ESP", then track ADDs and SUBs of |
| 49 | literal values to t_ESP, and the total delta of the ADDs/SUBs. Then if |
| 50 | "PUTL t_ESP, %ESP" happens, we call the helper with the known delta. We |
| 51 | also cope with "MOVL t_ESP, tX", making tX the new t_ESP. If any other |
| 52 | instruction clobbers t_ESP, we don't track it anymore, and fall back to |
| 53 | the delta-is-unknown case. That case is also used when the delta is not |
| 54 | a nice small amount, or an unknown amount. |
| 55 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 56 | |
| 57 | static |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 58 | IRBB* vg_SP_update_pass ( IRBB* bb_in, VexGuestLayout* layout, |
| 59 | IRType hWordTy ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 60 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 61 | Int i, j, minoff_ST, maxoff_ST, sizeof_SP, offset_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 62 | IRDirty *dcall, *d; |
| 63 | IRStmt* st; |
| 64 | IRExpr* e; |
| 65 | IRArray* descr; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 66 | IRTemp curr; |
| 67 | IRType typeof_SP; |
| 68 | Long delta; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 69 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 70 | /* Set up BB */ |
| 71 | IRBB* bb = emptyIRBB(); |
| 72 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 73 | bb->next = dopyIRExpr(bb_in->next); |
| 74 | bb->jumpkind = bb_in->jumpkind; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 75 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 76 | curr = IRTemp_INVALID; |
| 77 | delta = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 78 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 79 | sizeof_SP = layout->sizeof_SP; |
| 80 | offset_SP = layout->offset_SP; |
| 81 | typeof_SP = sizeof_SP==4 ? Ity_I32 : Ity_I64; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 82 | vg_assert(sizeof_SP == 4 || sizeof_SP == 8); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 83 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 84 | # define IS_ADD(op) (sizeof_SP==4 ? ((op)==Iop_Add32) : ((op)==Iop_Add64)) |
| 85 | # define IS_SUB(op) (sizeof_SP==4 ? ((op)==Iop_Sub32) : ((op)==Iop_Sub64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 86 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 87 | # define IS_ADD_OR_SUB(op) (IS_ADD(op) || IS_SUB(op)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 88 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 89 | # define GET_CONST(con) \ |
| 90 | (sizeof_SP==4 ? (Long)(Int)(con->Ico.U32) \ |
| 91 | : (Long)(con->Ico.U64)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 92 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 93 | # define DO(kind, syze) \ |
| 94 | do { \ |
| 95 | if (!VG_(defined_##kind##_mem_stack_##syze)()) \ |
| 96 | goto generic; \ |
| 97 | \ |
| 98 | /* I don't know if it's really necessary to say that the */ \ |
| 99 | /* call reads the stack pointer. But anyway, we do. */ \ |
| 100 | dcall = unsafeIRDirty_0_N( \ |
| 101 | 1/*regparms*/, \ |
| 102 | "track_" #kind "_mem_stack_" #syze, \ |
| 103 | VG_(tool_interface) \ |
| 104 | .track_##kind##_mem_stack_##syze, \ |
| 105 | mkIRExprVec_1(IRExpr_Tmp(curr)) \ |
| 106 | ); \ |
| 107 | dcall->nFxState = 1; \ |
| 108 | dcall->fxState[0].fx = Ifx_Read; \ |
| 109 | dcall->fxState[0].offset = layout->offset_SP; \ |
| 110 | dcall->fxState[0].size = layout->sizeof_SP; \ |
| 111 | \ |
| 112 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); \ |
| 113 | } while (0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 114 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 115 | for (i = 0; i < bb_in->stmts_used; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 116 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 117 | st = bb_in->stmts[i]; |
| 118 | if (!st) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 119 | continue; |
| 120 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 121 | /* t = Get(sp): curr = t, delta = 0 */ |
| 122 | if (st->tag != Ist_Tmp) goto case2; |
| 123 | e = st->Ist.Tmp.data; |
| 124 | if (e->tag != Iex_Get) goto case2; |
| 125 | if (e->Iex.Get.offset != offset_SP) goto case2; |
| 126 | if (e->Iex.Get.ty != typeof_SP) goto case2; |
| 127 | curr = st->Ist.Tmp.tmp; |
| 128 | delta = 0; |
| 129 | addStmtToIRBB( bb, st ); |
| 130 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 131 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 132 | case2: |
| 133 | /* t' = curr +/- const: curr = t', delta +=/-= const */ |
| 134 | if (st->tag != Ist_Tmp) goto case3; |
| 135 | e = st->Ist.Tmp.data; |
| 136 | if (e->tag != Iex_Binop) goto case3; |
| 137 | if (e->Iex.Binop.arg1->tag != Iex_Tmp) goto case3; |
| 138 | if (e->Iex.Binop.arg1->Iex.Tmp.tmp != curr) goto case3; |
| 139 | if (e->Iex.Binop.arg2->tag != Iex_Const) goto case3; |
| 140 | if (!IS_ADD_OR_SUB(e->Iex.Binop.op)) goto case3; |
| 141 | curr = st->Ist.Tmp.tmp; |
| 142 | if (IS_ADD(e->Iex.Binop.op)) |
| 143 | delta += GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 144 | else |
| 145 | delta -= GET_CONST(e->Iex.Binop.arg2->Iex.Const.con); |
| 146 | addStmtToIRBB( bb, st ); |
| 147 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 148 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 149 | case3: |
| 150 | /* t' = curr: curr = t' */ |
| 151 | if (st->tag != Ist_Tmp) goto case4; |
| 152 | e = st->Ist.Tmp.data; |
| 153 | if (e->tag != Iex_Tmp) goto case4; |
| 154 | if (e->Iex.Tmp.tmp != curr) goto case4; |
| 155 | curr = st->Ist.Tmp.tmp; |
| 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 | case4: |
| 160 | /* Put(sp) = curr */ |
| 161 | if (st->tag != Ist_Put) goto case5; |
| 162 | if (st->Ist.Put.offset != offset_SP) goto case5; |
| 163 | if (st->Ist.Put.data->tag != Iex_Tmp) goto case5; |
| 164 | if (st->Ist.Put.data->Iex.Tmp.tmp == curr) { |
| 165 | switch (delta) { |
| 166 | case 0: addStmtToIRBB(bb,st); delta = 0; continue; |
| 167 | case 4: DO(die, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 168 | case -4: DO(new, 4); addStmtToIRBB(bb,st); delta = 0; continue; |
| 169 | case 8: DO(die, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 170 | case -8: DO(new, 8); addStmtToIRBB(bb,st); delta = 0; continue; |
| 171 | case 12: DO(die, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 172 | case -12: DO(new, 12); addStmtToIRBB(bb,st); delta = 0; continue; |
| 173 | case 16: DO(die, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 174 | case -16: DO(new, 16); addStmtToIRBB(bb,st); delta = 0; continue; |
| 175 | case 32: DO(die, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 176 | case -32: DO(new, 32); addStmtToIRBB(bb,st); delta = 0; continue; |
| 177 | default: goto generic; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 178 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 179 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 180 | IRTemp old_SP; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 181 | generic: |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 182 | /* Pass both the old and new SP values to this helper. */ |
| 183 | old_SP = newIRTemp(bb->tyenv, typeof_SP); |
| 184 | addStmtToIRBB( |
| 185 | bb, |
| 186 | IRStmt_Tmp( old_SP, IRExpr_Get(offset_SP, typeof_SP) ) |
| 187 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 188 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 189 | dcall = unsafeIRDirty_0_N( |
| 190 | 2/*regparms*/, |
| 191 | "VG_(unknown_SP_update)", &VG_(unknown_SP_update), |
| 192 | mkIRExprVec_2( IRExpr_Tmp(old_SP), st->Ist.Put.data ) |
| 193 | ); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 194 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 195 | |
| 196 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 197 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 198 | curr = st->Ist.Put.data->Iex.Tmp.tmp; |
| 199 | delta = 0; |
| 200 | continue; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 201 | } |
| 202 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 203 | case5: |
| 204 | /* PutI or Dirty call which overlaps SP: complain. We can't |
| 205 | deal with SP changing in weird ways (well, we can, but not at |
| 206 | this time of night). */ |
| 207 | if (st->tag == Ist_PutI) { |
| 208 | descr = st->Ist.PutI.descr; |
| 209 | minoff_ST = descr->base; |
| 210 | maxoff_ST = descr->base + descr->nElems * sizeofIRType(descr->elemTy) - 1; |
| 211 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 212 | goto complain; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 213 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 214 | if (st->tag == Ist_Dirty) { |
| 215 | d = st->Ist.Dirty.details; |
| 216 | for (j = 0; j < d->nFxState; j++) { |
| 217 | minoff_ST = d->fxState[j].offset; |
| 218 | maxoff_ST = d->fxState[j].offset + d->fxState[j].size - 1; |
| 219 | if (d->fxState[j].fx == Ifx_Read || d->fxState[j].fx == Ifx_None) |
| 220 | continue; |
| 221 | if (!(offset_SP > maxoff_ST || (offset_SP + sizeof_SP - 1) < minoff_ST)) |
| 222 | goto complain; |
| 223 | } |
| 224 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 225 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 226 | /* well, not interesting. Just copy and keep going. */ |
| 227 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 228 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 229 | } /* for (i = 0; i < bb_in->stmts_used; i++) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 230 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 231 | return bb; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 232 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 233 | complain: |
| 234 | VG_(core_panic)("vg_SP_update_pass: PutI or Dirty which overlaps SP"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 235 | |
| 236 | } |
sewardj | 7c4b604 | 2003-06-14 15:47:15 +0000 | [diff] [blame] | 237 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 238 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 239 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 240 | #if 0 |
| 241 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 242 | st = bb_in->stmts[i]; |
| 243 | if (!st) |
| 244 | continue; |
| 245 | if (st->tag != Ist_Put) |
| 246 | goto boring; |
| 247 | offP = st->Ist.Put.offset; |
| 248 | if (offP != layout->offset_SP) |
| 249 | goto boring; |
| 250 | szP = sizeofIRType(typeOfIRExpr(bb_in->tyenv, st->Ist.Put.data)); |
| 251 | if (szP != layout->sizeof_SP) |
| 252 | goto boring; |
| 253 | vg_assert(isAtom(st->Ist.Put.data)); |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 254 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 255 | /* I don't know if it's really necessary to say that the call reads |
| 256 | the stack pointer. But anyway, we do. */ |
| 257 | dcall = unsafeIRDirty_0_N( |
| 258 | mkIRCallee(1, "VG_(unknown_esp_update)", |
| 259 | (HWord)&VG_(unknown_esp_update)), |
| 260 | mkIRExprVec_1(st->Ist.Put.data) |
| 261 | ); |
| 262 | dcall->nFxState = 1; |
| 263 | dcall->fxState[0].fx = Ifx_Read; |
| 264 | dcall->fxState[0].offset = layout->offset_SP; |
| 265 | dcall->fxState[0].size = layout->sizeof_SP; |
sewardj | 97ced73 | 2002-03-25 00:07:36 +0000 | [diff] [blame] | 266 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 267 | addStmtToIRBB( bb, IRStmt_Dirty(dcall) ); |
| 268 | |
| 269 | boring: |
| 270 | addStmtToIRBB( bb, st ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 271 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 272 | #endif |
| 273 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 274 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 275 | /*------------------------------------------------------------*/ |
| 276 | /*--- Main entry point for the JITter. ---*/ |
| 277 | /*------------------------------------------------------------*/ |
| 278 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 279 | /* Vex dumps the final code in here. Then we can copy it off |
| 280 | wherever we like. */ |
| 281 | #define N_TMPBUF 10000 |
| 282 | static UChar tmpbuf[N_TMPBUF]; |
| 283 | |
| 284 | /* Function pointers we must supply to LibVEX in order that it |
| 285 | can bomb out and emit messages under Valgrind's control. */ |
| 286 | __attribute__ ((noreturn)) |
| 287 | static |
| 288 | void failure_exit ( void ) |
| 289 | { |
| 290 | LibVEX_ClearTemporary(True); |
sewardj | c621f76 | 2004-11-26 13:49:59 +0000 | [diff] [blame] | 291 | VG_(core_panic)("LibVEX called failure_exit()."); |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static |
| 295 | void log_bytes ( Char* bytes, Int nbytes ) |
| 296 | { |
| 297 | Int i; |
| 298 | for (i = 0; i < nbytes-3; i += 4) |
| 299 | VG_(printf)("%c%c%c%c", bytes[i], bytes[i+1], bytes[i+2], bytes[i+3]); |
| 300 | for (; i < nbytes; i++) |
| 301 | VG_(printf)("%c", bytes[i]); |
| 302 | } |
| 303 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 304 | /* Translate the basic block beginning at orig_addr, and add it to |
| 305 | the translation cache & translation table. Unless 'debugging' is true, |
| 306 | in which case the call is being done for debugging purposes, so |
| 307 | (a) throw away the translation once it is made, and (b) produce a |
| 308 | load of debugging output. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 309 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 310 | 'tid' is the identity of the thread needing this block. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 311 | */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 312 | |
| 313 | /* This stops Vex from chasing into function entry points that we wish |
| 314 | to redirect. Chasing across them obviously defeats the redirect |
| 315 | mechanism, with bad effects for Memcheck, Addrcheck, and possibly |
| 316 | others. */ |
| 317 | static Bool chase_into_ok ( Addr64 addr64 ) |
| 318 | { |
| 319 | Addr addr = (Addr)addr64; |
| 320 | if (addr != VG_(code_redirect)(addr)) { |
| 321 | if (0) VG_(printf)("not chasing into 0x%x\n", addr); |
| 322 | return False; |
| 323 | } else { |
| 324 | return True; /* ok to chase into 'addr' */ |
| 325 | } |
| 326 | } |
| 327 | |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 328 | static Bool need_to_handle_SP_assignment(void) |
| 329 | { |
| 330 | return ( VG_(defined_new_mem_stack_4)() || |
| 331 | VG_(defined_die_mem_stack_4)() || |
| 332 | VG_(defined_new_mem_stack_8)() || |
| 333 | VG_(defined_die_mem_stack_8)() || |
| 334 | VG_(defined_new_mem_stack_12)() || |
| 335 | VG_(defined_die_mem_stack_12)() || |
| 336 | VG_(defined_new_mem_stack_16)() || |
| 337 | VG_(defined_die_mem_stack_16)() || |
| 338 | VG_(defined_new_mem_stack_32)() || |
| 339 | VG_(defined_die_mem_stack_32)() || |
| 340 | VG_(defined_new_mem_stack)() || |
| 341 | VG_(defined_die_mem_stack)() |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 346 | Bool VG_(translate) ( ThreadId tid, Addr orig_addr, |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 347 | Bool debugging_translation ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 348 | { |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 349 | Addr redir, orig_addr0 = orig_addr; |
| 350 | Int orig_size, tmpbuf_used, verbosity; |
| 351 | Bool notrace_until_done; |
| 352 | UInt notrace_until_limit = 0; |
| 353 | Segment* seg; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 354 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 355 | /* Make sure Vex is initialised right. */ |
| 356 | TranslateResult tres; |
| 357 | static Bool vex_init_done = False; |
| 358 | |
| 359 | if (!vex_init_done) { |
| 360 | LibVEX_Init ( &failure_exit, &log_bytes, |
| 361 | 1, /* debug_paranoia */ |
| 362 | False, /* valgrind support */ |
| 363 | &VG_(clo_vex_control) ); |
| 364 | vex_init_done = True; |
| 365 | } |
| 366 | |
| 367 | /* profiling ... */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 368 | VGP_PUSHCC(VgpTranslate); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 369 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 370 | /* Look in the code redirect table to see if we should |
| 371 | translate an alternative address for orig_addr. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 372 | redir = VG_(code_redirect)(orig_addr); |
| 373 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 374 | if (redir != orig_addr && VG_(clo_verbosity) >= 2) { |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 375 | Char name1[64] = ""; |
| 376 | Char name2[64] = ""; |
| 377 | VG_(get_fnname_w_offset)(orig_addr, name1, 64); |
| 378 | VG_(get_fnname_w_offset)(redir, name2, 64); |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 379 | name1[63] = name2[63] = 0; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 380 | VG_(message)(Vg_UserMsg, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 381 | "TRANSLATE: %p (%s) redirected to %p (%s)", |
| 382 | orig_addr, name1, |
| 383 | redir, name2 ); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 384 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 385 | orig_addr = redir; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 386 | |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 387 | /* If codegen tracing, don't start tracing until |
| 388 | notrace_until_limit blocks have gone by. This avoids printing |
| 389 | huge amounts of useless junk when all we want to see is the last |
| 390 | few blocks translated prior to a failure. Set |
| 391 | notrace_until_limit to be the number of translations to be made |
| 392 | before --trace-codegen= style printing takes effect. */ |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 393 | notrace_until_done = VG_(get_bbs_translated)() >= notrace_until_limit; |
sewardj | a60be0e | 2003-05-26 08:47:27 +0000 | [diff] [blame] | 394 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 395 | seg = VG_(find_segment)(orig_addr); |
| 396 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 397 | if (!debugging_translation) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 398 | VG_TRACK( pre_mem_read, Vg_CoreTranslate, tid, "", orig_addr, 1 ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 399 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 400 | if (seg == NULL || |
| 401 | !VG_(seg_contains)(seg, orig_addr, 1) || |
| 402 | (seg->prot & (VKI_PROT_READ|VKI_PROT_EXEC)) == 0) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 403 | /* Code address is bad - deliver a signal instead */ |
| 404 | vg_assert(!VG_(is_addressable)(orig_addr, 1)); |
| 405 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 406 | if (seg != NULL && VG_(seg_contains)(seg, orig_addr, 1)) { |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 407 | vg_assert((seg->prot & VKI_PROT_EXEC) == 0); |
| 408 | VG_(synth_fault_perms)(tid, orig_addr); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 409 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 410 | VG_(synth_fault_mapping)(tid, orig_addr); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 411 | |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 412 | return False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 413 | } else |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 414 | seg->flags |= SF_CODE; /* contains cached code */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 415 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 416 | /* If doing any code printing, print a basic block start marker */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 417 | if (VG_(clo_trace_codegen)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 418 | Char fnname[64] = ""; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 419 | VG_(get_fnname_w_offset)(orig_addr, fnname, 64); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 420 | VG_(printf)( |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 421 | "==== BB %d %s(%p) approx BBs exec'd %llu ====\n", |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 422 | VG_(get_bbs_translated)(), fnname, orig_addr, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 423 | VG_(bbs_done)); |
| 424 | } |
| 425 | |
| 426 | /* 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] | 427 | verbosity = 0; |
| 428 | if ( debugging_translation |
| 429 | || (VG_(clo_trace_codegen) > 0 |
| 430 | && VG_(get_bbs_translated)() >= VG_(clo_trace_notbelow) )) |
| 431 | verbosity = VG_(clo_trace_codegen); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 432 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 433 | /* Actually do the translation. */ |
| 434 | tres = LibVEX_Translate ( |
njn | 09aa128 | 2004-11-30 23:39:13 +0000 | [diff] [blame] | 435 | InsnSetArch, InsnSetArch, |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 436 | (Char*)orig_addr, (Addr64)orig_addr, |
| 437 | chase_into_ok, |
| 438 | &orig_size, |
| 439 | tmpbuf, N_TMPBUF, &tmpbuf_used, |
| 440 | TL_(instrument), |
njn | bd2e75c | 2004-11-24 16:43:43 +0000 | [diff] [blame] | 441 | need_to_handle_SP_assignment() |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 442 | ? vg_SP_update_pass |
| 443 | : NULL, |
| 444 | True, /* cleanup after instrumentation */ |
| 445 | NULL, |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 446 | verbosity |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 447 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 448 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 449 | vg_assert(tres == TransOK); |
| 450 | vg_assert(tmpbuf_used <= N_TMPBUF); |
| 451 | vg_assert(tmpbuf_used > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 452 | |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 453 | #undef DECIDE_IF_PRINTING_CODEGEN |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 454 | |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 455 | /* Copy data at trans_addr into the translation cache. */ |
| 456 | /* Since the .orig_size and .trans_size fields are UShort, be paranoid. */ |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 457 | vg_assert(orig_size > 0 && orig_size < 65536); |
| 458 | vg_assert(tmpbuf_used > 0 && tmpbuf_used < 65536); |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 459 | |
| 460 | // If debugging, don't do anything with the translated block; we |
| 461 | // only did this for the debugging output produced along the way. |
| 462 | if (!debugging_translation) { |
| 463 | // Note that we use orig_addr0, not orig_addr, which might have been |
| 464 | // changed by the redirection |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 465 | VG_(add_to_trans_tab)( orig_addr0, orig_size, |
| 466 | (Addr)(&tmpbuf[0]), tmpbuf_used ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 467 | } |
nethercote | 59a122d | 2004-08-03 17:16:51 +0000 | [diff] [blame] | 468 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 469 | VGP_POPCC(VgpTranslate); |
nethercote | 4d71438 | 2004-10-13 09:47:24 +0000 | [diff] [blame] | 470 | |
| 471 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /*--------------------------------------------------------------------*/ |
| 475 | /*--- end vg_translate.c ---*/ |
| 476 | /*--------------------------------------------------------------------*/ |
njn | ed61971 | 2003-10-01 16:45:04 +0000 | [diff] [blame] | 477 | |