nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 3 | /*--- Instrument UCode to perform memory checking operations. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- mc_translate.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 6 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | detecting memory errors. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 10 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2004 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | jseward@acm.org |
| 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 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 32 | #include "mc_include.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 33 | |
| 34 | /* --------------------------------------------------------------------- |
| 35 | Template functions for extending UCode |
| 36 | ------------------------------------------------------------------ */ |
| 37 | |
| 38 | /* Compare this with the restrictions on core instructions in |
| 39 | vg_translate.c:VG_(saneUInstr)(). Everything general said there applies |
| 40 | here too. |
| 41 | */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 42 | Bool SK_(sane_XUInstr)(Bool beforeRA, Bool beforeLiveness, UInstr* u) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | { |
| 44 | // SSS: duplicating these macros really sucks |
| 45 | # define LIT0 (u->lit32 == 0) |
| 46 | # define LIT1 (!(LIT0)) |
| 47 | # define LITm (u->tag1 == Literal ? True : LIT0 ) |
| 48 | # define SZ0 (u->size == 0) |
| 49 | # define SZi (u->size == 4 || u->size == 2 || u->size == 1) |
| 50 | # define SZj (u->size == 4 || u->size == 2 || u->size == 1 || u->size == 0) |
| 51 | # define CC0 (u->flags_r == FlagsEmpty && u->flags_w == FlagsEmpty) |
| 52 | # define TR1 (beforeRA ? (u->tag1 == TempReg) : (u->tag1 == RealReg)) |
| 53 | # define TR2 (beforeRA ? (u->tag2 == TempReg) : (u->tag2 == RealReg)) |
| 54 | # define A1 (u->tag1 == ArchReg) |
| 55 | # define A2 (u->tag2 == ArchReg) |
| 56 | # define L1 (u->tag1 == Literal && u->val1 == 0) |
| 57 | # define Ls1 (u->tag1 == Lit16) |
| 58 | # define Ls3 (u->tag3 == Lit16) |
| 59 | # define TRL1 (TR1 || L1) |
| 60 | # define TRA1 (TR1 || A1) |
| 61 | # define N2 (u->tag2 == NoValue) |
| 62 | # define N3 (u->tag3 == NoValue) |
| 63 | # define COND0 (u->cond == 0) |
| 64 | # define EXTRA4b0 (u->extra4b == 0) |
| 65 | # define SG_WD0 (u->signed_widen == 0) |
| 66 | # define JMPKIND0 (u->jmpkind == 0) |
| 67 | # define CCALL0 (u->argc==0 && u->regparms_n==0 && u->has_ret_val==0 && \ |
| 68 | ( beforeLiveness \ |
| 69 | ? u->regs_live_after == ALL_RREGS_LIVE \ |
| 70 | : True )) |
| 71 | # define XOTHER (COND0 && EXTRA4b0 && SG_WD0 && JMPKIND0 && CCALL0) |
| 72 | |
| 73 | Int n_lits = 0; |
| 74 | if (u->tag1 == Literal) n_lits++; |
| 75 | if (u->tag2 == Literal) n_lits++; |
| 76 | if (u->tag3 == Literal) n_lits++; |
| 77 | if (n_lits > 1) |
| 78 | return False; |
| 79 | |
| 80 | /* Fields not checked: val1, val2, val3 */ |
| 81 | |
| 82 | switch (u->opcode) { |
| 83 | |
| 84 | /* Fields checked: lit32 size flags_r/w tag1 tag2 tag3 (rest) */ |
| 85 | case LOADV: return LIT0 && SZi && CC0 && TR1 && TR2 && N3 && XOTHER; |
| 86 | case STOREV: return LITm && SZi && CC0 && TRL1 && TR2 && N3 && XOTHER; |
| 87 | case GETV: return LIT0 && SZi && CC0 && A1 && TR2 && N3 && XOTHER; |
| 88 | case PUTV: return LITm && SZi && CC0 && TRL1 && A2 && N3 && XOTHER; |
| 89 | case GETVF: |
| 90 | case PUTVF: return LIT0 && SZ0 && CC0 && TR1 && N2 && N3 && XOTHER; |
| 91 | case TESTV: |
| 92 | case SETV: return LIT0 && SZj && CC0 && TRA1 && N2 && N3 && XOTHER; |
| 93 | case TAG1: return LIT0 && SZ0 && CC0 && TR1 && N2 && Ls3 && XOTHER; |
| 94 | case TAG2: return LIT0 && SZ0 && CC0 && TR1 && TR2 && Ls3 && XOTHER; |
| 95 | default: |
| 96 | VG_(printf)("unhandled opcode: %u\n", u->opcode); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 97 | VG_(skin_panic)("SK_(sane_XUInstr): unhandled opcode"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 98 | } |
| 99 | # undef LIT0 |
| 100 | # undef LIT1 |
| 101 | # undef LITm |
| 102 | # undef SZ0 |
| 103 | # undef SZi |
| 104 | # undef SZj |
| 105 | # undef CC0 |
| 106 | # undef TR1 |
| 107 | # undef TR2 |
| 108 | # undef A1 |
| 109 | # undef A2 |
| 110 | # undef L1 |
| 111 | # undef Ls1 |
| 112 | # undef Ls3 |
| 113 | # undef TRL1 |
| 114 | # undef TRA1 |
| 115 | # undef N2 |
| 116 | # undef N3 |
| 117 | # undef COND0 |
| 118 | # undef EXTRA4b0 |
| 119 | # undef JMPKIND0 |
| 120 | # undef CCALL0 |
| 121 | # undef XOTHER |
| 122 | } |
| 123 | |
| 124 | static Char* nameOfTagOp ( TagOp h ) |
| 125 | { |
| 126 | switch (h) { |
| 127 | case Tag_PCast40: return "PCast40"; |
| 128 | case Tag_PCast20: return "PCast20"; |
| 129 | case Tag_PCast10: return "PCast10"; |
| 130 | case Tag_PCast01: return "PCast01"; |
| 131 | case Tag_PCast02: return "PCast02"; |
| 132 | case Tag_PCast04: return "PCast04"; |
| 133 | case Tag_PCast14: return "PCast14"; |
| 134 | case Tag_PCast12: return "PCast12"; |
| 135 | case Tag_PCast11: return "PCast11"; |
| 136 | case Tag_Left4: return "Left4"; |
| 137 | case Tag_Left2: return "Left2"; |
| 138 | case Tag_Left1: return "Left1"; |
| 139 | case Tag_SWiden14: return "SWiden14"; |
| 140 | case Tag_SWiden24: return "SWiden24"; |
| 141 | case Tag_SWiden12: return "SWiden12"; |
| 142 | case Tag_ZWiden14: return "ZWiden14"; |
| 143 | case Tag_ZWiden24: return "ZWiden24"; |
| 144 | case Tag_ZWiden12: return "ZWiden12"; |
| 145 | case Tag_UifU4: return "UifU4"; |
| 146 | case Tag_UifU2: return "UifU2"; |
| 147 | case Tag_UifU1: return "UifU1"; |
| 148 | case Tag_UifU0: return "UifU0"; |
| 149 | case Tag_DifD4: return "DifD4"; |
| 150 | case Tag_DifD2: return "DifD2"; |
| 151 | case Tag_DifD1: return "DifD1"; |
| 152 | case Tag_ImproveAND4_TQ: return "ImproveAND4_TQ"; |
| 153 | case Tag_ImproveAND2_TQ: return "ImproveAND2_TQ"; |
| 154 | case Tag_ImproveAND1_TQ: return "ImproveAND1_TQ"; |
| 155 | case Tag_ImproveOR4_TQ: return "ImproveOR4_TQ"; |
| 156 | case Tag_ImproveOR2_TQ: return "ImproveOR2_TQ"; |
| 157 | case Tag_ImproveOR1_TQ: return "ImproveOR1_TQ"; |
| 158 | case Tag_DebugFn: return "DebugFn"; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 159 | default: VG_(skin_panic)("vg_nameOfTagOp"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 164 | Char* SK_(name_XUOpcode)(Opcode opc) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | { |
| 166 | switch (opc) { |
| 167 | case GETVF: return "GETVF"; |
| 168 | case PUTVF: return "PUTVF"; |
| 169 | case TAG1: return "TAG1"; |
| 170 | case TAG2: return "TAG2"; |
| 171 | case LOADV: return "LOADV"; |
| 172 | case STOREV: return "STOREV"; |
| 173 | case GETV: return "GETV"; |
| 174 | case PUTV: return "PUTV"; |
| 175 | case TESTV: return "TESTV"; |
| 176 | case SETV: return "SETV"; |
| 177 | default: |
| 178 | VG_(printf)("unhandled opcode: %u\n", opc); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 179 | VG_(skin_panic)("SK_(name_XUOpcode): unhandled case"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /* --------------------------------------------------------------------- |
| 184 | Debugging stuff. |
| 185 | ------------------------------------------------------------------ */ |
| 186 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 187 | void SK_(pp_XUInstr)(UInstr* u) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 188 | { |
| 189 | switch (u->opcode) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 190 | case TAG1: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 191 | VG_(pp_UOperand)(u, 1, 4, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 192 | VG_(printf)(" = %s ( ", nameOfTagOp( u->val3 )); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 193 | VG_(pp_UOperand)(u, 1, 4, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 194 | VG_(printf)(" )"); |
| 195 | break; |
| 196 | |
| 197 | case TAG2: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 198 | VG_(pp_UOperand)(u, 2, 4, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 199 | VG_(printf)(" = %s ( ", nameOfTagOp( u->val3 )); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 200 | VG_(pp_UOperand)(u, 1, 4, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 201 | VG_(printf)(", "); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 202 | VG_(pp_UOperand)(u, 2, 4, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 203 | VG_(printf)(" )"); |
| 204 | break; |
| 205 | |
| 206 | case STOREV: case LOADV: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 207 | VG_(pp_UOperand)(u, 1, u->size, u->opcode==LOADV); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 208 | VG_(printf)(", "); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 209 | VG_(pp_UOperand)(u, 2, u->size, u->opcode==STOREV); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 210 | break; |
| 211 | |
| 212 | case PUTVF: case GETVF: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 213 | VG_(pp_UOperand)(u, 1, 0, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | case GETV: case PUTV: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 217 | VG_(pp_UOperand)(u, 1, u->opcode==PUTV ? 4 : u->size, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 218 | VG_(printf)(", "); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 219 | VG_(pp_UOperand)(u, 2, u->opcode==GETV ? 4 : u->size, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 220 | break; |
| 221 | |
| 222 | case TESTV: case SETV: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 223 | VG_(pp_UOperand)(u, 1, u->size, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | default: |
| 227 | VG_(printf)("unhandled opcode: %u\n", u->opcode); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 228 | VG_(skin_panic)("SK_(pp_XUInstr): unhandled opcode"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } |
| 232 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 233 | Int SK_(get_Xreg_usage)(UInstr* u, Tag tag, Int* regs, Bool* isWrites) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 234 | { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 235 | # define RD(ono) VG_UINSTR_READS_REG(ono, regs, isWrites) |
| 236 | # define WR(ono) VG_UINSTR_WRITES_REG(ono, regs, isWrites) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 237 | |
| 238 | Int n = 0; |
| 239 | switch (u->opcode) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 240 | case TAG1: RD(1); WR(1); break; |
| 241 | case TAG2: RD(1); RD(2); WR(2); break; |
| 242 | case LOADV: RD(1); WR(2); break; |
| 243 | case STOREV: RD(1); RD(2); break; |
| 244 | case GETV: WR(2); break; |
| 245 | case PUTV: RD(1); break; |
| 246 | case TESTV: RD(1); break; |
| 247 | case SETV: WR(1); break; |
| 248 | case PUTVF: RD(1); break; |
| 249 | case GETVF: WR(1); break; |
| 250 | |
| 251 | default: |
| 252 | VG_(printf)("unhandled opcode: %u\n", u->opcode); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 253 | VG_(skin_panic)("SK_(get_Xreg_usage): unhandled opcode"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 254 | } |
| 255 | return n; |
| 256 | |
| 257 | # undef RD |
| 258 | # undef WR |
| 259 | } |
| 260 | |
| 261 | /*------------------------------------------------------------*/ |
| 262 | /*--- New instrumentation machinery. ---*/ |
| 263 | /*------------------------------------------------------------*/ |
| 264 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 265 | static |
| 266 | TagOp get_Tag_ImproveOR_TQ ( Int sz ) |
| 267 | { |
| 268 | switch (sz) { |
| 269 | case 4: return Tag_ImproveOR4_TQ; |
| 270 | case 2: return Tag_ImproveOR2_TQ; |
| 271 | case 1: return Tag_ImproveOR1_TQ; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 272 | default: VG_(skin_panic)("get_Tag_ImproveOR_TQ"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static |
| 278 | TagOp get_Tag_ImproveAND_TQ ( Int sz ) |
| 279 | { |
| 280 | switch (sz) { |
| 281 | case 4: return Tag_ImproveAND4_TQ; |
| 282 | case 2: return Tag_ImproveAND2_TQ; |
| 283 | case 1: return Tag_ImproveAND1_TQ; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 284 | default: VG_(skin_panic)("get_Tag_ImproveAND_TQ"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | static |
| 290 | TagOp get_Tag_Left ( Int sz ) |
| 291 | { |
| 292 | switch (sz) { |
| 293 | case 4: return Tag_Left4; |
| 294 | case 2: return Tag_Left2; |
| 295 | case 1: return Tag_Left1; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 296 | default: VG_(skin_panic)("get_Tag_Left"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static |
| 302 | TagOp get_Tag_UifU ( Int sz ) |
| 303 | { |
| 304 | switch (sz) { |
| 305 | case 4: return Tag_UifU4; |
| 306 | case 2: return Tag_UifU2; |
| 307 | case 1: return Tag_UifU1; |
| 308 | case 0: return Tag_UifU0; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 309 | default: VG_(skin_panic)("get_Tag_UifU"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static |
| 315 | TagOp get_Tag_DifD ( Int sz ) |
| 316 | { |
| 317 | switch (sz) { |
| 318 | case 4: return Tag_DifD4; |
| 319 | case 2: return Tag_DifD2; |
| 320 | case 1: return Tag_DifD1; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 321 | default: VG_(skin_panic)("get_Tag_DifD"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static |
| 327 | TagOp get_Tag_PCast ( Int szs, Int szd ) |
| 328 | { |
| 329 | if (szs == 4 && szd == 0) return Tag_PCast40; |
| 330 | if (szs == 2 && szd == 0) return Tag_PCast20; |
| 331 | if (szs == 1 && szd == 0) return Tag_PCast10; |
| 332 | if (szs == 0 && szd == 1) return Tag_PCast01; |
| 333 | if (szs == 0 && szd == 2) return Tag_PCast02; |
| 334 | if (szs == 0 && szd == 4) return Tag_PCast04; |
| 335 | if (szs == 1 && szd == 4) return Tag_PCast14; |
| 336 | if (szs == 1 && szd == 2) return Tag_PCast12; |
| 337 | if (szs == 1 && szd == 1) return Tag_PCast11; |
| 338 | VG_(printf)("get_Tag_PCast(%d,%d)\n", szs, szd); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 339 | VG_(skin_panic)("get_Tag_PCast"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
| 343 | static |
| 344 | TagOp get_Tag_Widen ( Bool syned, Int szs, Int szd ) |
| 345 | { |
| 346 | if (szs == 1 && szd == 2 && syned) return Tag_SWiden12; |
| 347 | if (szs == 1 && szd == 2 && !syned) return Tag_ZWiden12; |
| 348 | |
| 349 | if (szs == 1 && szd == 4 && syned) return Tag_SWiden14; |
| 350 | if (szs == 1 && szd == 4 && !syned) return Tag_ZWiden14; |
| 351 | |
| 352 | if (szs == 2 && szd == 4 && syned) return Tag_SWiden24; |
| 353 | if (szs == 2 && szd == 4 && !syned) return Tag_ZWiden24; |
| 354 | |
| 355 | VG_(printf)("get_Tag_Widen(%d,%d,%d)\n", (Int)syned, szs, szd); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 356 | VG_(skin_panic)("get_Tag_Widen"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* Pessimally cast the spec'd shadow from one size to another. */ |
| 360 | static |
| 361 | void create_PCast ( UCodeBlock* cb, Int szs, Int szd, Int tempreg ) |
| 362 | { |
| 363 | if (szs == 0 && szd == 0) |
| 364 | return; |
| 365 | uInstr3(cb, TAG1, 0, TempReg, tempreg, |
| 366 | NoValue, 0, |
| 367 | Lit16, get_Tag_PCast(szs,szd)); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /* Create a signed or unsigned widen of the spec'd shadow from one |
| 372 | size to another. The only allowed size transitions are 1->2, 1->4 |
| 373 | and 2->4. */ |
| 374 | static |
| 375 | void create_Widen ( UCodeBlock* cb, Bool signed_widen, |
| 376 | Int szs, Int szd, Int tempreg ) |
| 377 | { |
| 378 | if (szs == szd) return; |
| 379 | uInstr3(cb, TAG1, 0, TempReg, tempreg, |
| 380 | NoValue, 0, |
| 381 | Lit16, get_Tag_Widen(signed_widen,szs,szd)); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* Get the condition codes into a new shadow, at the given size. */ |
| 386 | static |
| 387 | Int create_GETVF ( UCodeBlock* cb, Int sz ) |
| 388 | { |
| 389 | Int tt = newShadow(cb); |
| 390 | uInstr1(cb, GETVF, 0, TempReg, tt); |
| 391 | create_PCast(cb, 0, sz, tt); |
| 392 | return tt; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* Save the condition codes from the spec'd shadow. */ |
| 397 | static |
| 398 | void create_PUTVF ( UCodeBlock* cb, Int sz, Int tempreg ) |
| 399 | { |
| 400 | if (sz == 0) { |
| 401 | uInstr1(cb, PUTVF, 0, TempReg, tempreg); |
| 402 | } else { |
| 403 | Int tt = newShadow(cb); |
| 404 | uInstr2(cb, MOV, 4, TempReg, tempreg, TempReg, tt); |
| 405 | create_PCast(cb, sz, 0, tt); |
| 406 | uInstr1(cb, PUTVF, 0, TempReg, tt); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /* Do Left on the spec'd shadow. */ |
| 412 | static |
| 413 | void create_Left ( UCodeBlock* cb, Int sz, Int tempreg ) |
| 414 | { |
| 415 | uInstr3(cb, TAG1, 0, |
| 416 | TempReg, tempreg, |
| 417 | NoValue, 0, |
| 418 | Lit16, get_Tag_Left(sz)); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /* Do UifU on ts and td, putting the result in td. */ |
| 423 | static |
| 424 | void create_UifU ( UCodeBlock* cb, Int sz, Int ts, Int td ) |
| 425 | { |
| 426 | uInstr3(cb, TAG2, 0, TempReg, ts, TempReg, td, |
| 427 | Lit16, get_Tag_UifU(sz)); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | /* Do DifD on ts and td, putting the result in td. */ |
| 432 | static |
| 433 | void create_DifD ( UCodeBlock* cb, Int sz, Int ts, Int td ) |
| 434 | { |
| 435 | uInstr3(cb, TAG2, 0, TempReg, ts, TempReg, td, |
| 436 | Lit16, get_Tag_DifD(sz)); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /* Do HelpAND on value tval and tag tqqq, putting the result in |
| 441 | tqqq. */ |
| 442 | static |
| 443 | void create_ImproveAND_TQ ( UCodeBlock* cb, Int sz, Int tval, Int tqqq ) |
| 444 | { |
| 445 | uInstr3(cb, TAG2, 0, TempReg, tval, TempReg, tqqq, |
| 446 | Lit16, get_Tag_ImproveAND_TQ(sz)); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* Do HelpOR on value tval and tag tqqq, putting the result in |
| 451 | tqqq. */ |
| 452 | static |
| 453 | void create_ImproveOR_TQ ( UCodeBlock* cb, Int sz, Int tval, Int tqqq ) |
| 454 | { |
| 455 | uInstr3(cb, TAG2, 0, TempReg, tval, TempReg, tqqq, |
| 456 | Lit16, get_Tag_ImproveOR_TQ(sz)); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /* Get the shadow for an operand described by (tag, val). Emit code |
| 461 | to do this and return the identity of the shadow holding the |
| 462 | result. The result tag is always copied into a new shadow, so it |
| 463 | can be modified without trashing the original.*/ |
| 464 | static |
| 465 | Int /* TempReg */ getOperandShadow ( UCodeBlock* cb, |
| 466 | Int sz, Int tag, Int val ) |
| 467 | { |
| 468 | Int sh; |
| 469 | sh = newShadow(cb); |
| 470 | if (tag == TempReg) { |
| 471 | uInstr2(cb, MOV, 4, TempReg, SHADOW(val), TempReg, sh); |
| 472 | return sh; |
| 473 | } |
| 474 | if (tag == Literal) { |
| 475 | uInstr1(cb, SETV, sz, TempReg, sh); |
| 476 | return sh; |
| 477 | } |
| 478 | if (tag == ArchReg) { |
| 479 | uInstr2(cb, GETV, sz, ArchReg, val, TempReg, sh); |
| 480 | return sh; |
| 481 | } |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 482 | VG_(skin_panic)("getOperandShadow"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* Create and return an instrumented version of cb_in. Free cb_in |
| 486 | before returning. */ |
| 487 | static UCodeBlock* memcheck_instrument ( UCodeBlock* cb_in ) |
| 488 | { |
| 489 | UCodeBlock* cb; |
| 490 | Int i, j; |
| 491 | UInstr* u_in; |
| 492 | Int qs, qd, qt, qtt; |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 493 | Bool bogusLiterals; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 494 | |
| 495 | cb = VG_(setup_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 496 | |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 497 | /* Scan the block to look for bogus literals. These are magic |
| 498 | numbers which particularly appear in hand-optimised / inlined |
| 499 | implementations of strlen() et al which cause so much trouble |
| 500 | (spurious reports of uninit-var uses). Purpose of this horrible |
| 501 | hack is to disable some checks any such literals are present in |
| 502 | this basic block. */ |
| 503 | bogusLiterals = False; |
| 504 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 505 | if (MC_(clo_avoid_strlen_errors)) { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 506 | for (i = 0; i < VG_(get_num_instrs)(cb_in); i++) { |
| 507 | u_in = VG_(get_instr)(cb_in, i); |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 508 | switch (u_in->opcode) { |
| 509 | case ADD: case SUB: case MOV: |
| 510 | if (u_in->size == 4 && u_in->tag1 == Literal) |
| 511 | goto literal; |
| 512 | break; |
| 513 | case LEA1: |
| 514 | sk_assert(u_in->size == 4); |
| 515 | goto literal; |
| 516 | default: |
| 517 | break; |
| 518 | } |
| 519 | continue; |
| 520 | literal: |
| 521 | if (u_in->lit32 == 0xFEFEFEFF || |
| 522 | u_in->lit32 == 0x80808080 || |
| 523 | u_in->lit32 == 0x00008080) { |
| 524 | bogusLiterals = True; |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 530 | for (i = 0; i < VG_(get_num_instrs)(cb_in); i++) { |
| 531 | u_in = VG_(get_instr)(cb_in, i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 532 | qs = qd = qt = qtt = INVALID_TEMPREG; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 533 | |
| 534 | switch (u_in->opcode) { |
| 535 | |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 536 | case LOCK: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 537 | case NOP: |
| 538 | break; |
| 539 | |
| 540 | case INCEIP: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 541 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 542 | break; |
| 543 | |
sewardj | d077f53 | 2002-09-30 21:52:50 +0000 | [diff] [blame] | 544 | /* The segment registers do not have their definedness |
| 545 | tracked. We therefore make fake shadows on GETSEG and |
| 546 | test them on PUTSEG. This will catch writing garbage to a |
| 547 | segment register; therefore we can assume it to be defined |
| 548 | when read (GETSEGd). Since the first arg of USESEG is |
| 549 | fetched by GETSEG, we can assume it to be defined, and so |
| 550 | the definedness of the result is simply the definedness of |
| 551 | the second (virtual_address) arg of USESEG. The upshot of |
| 552 | all this is that instrumentation of USESEG is a no-op! */ |
| 553 | |
| 554 | case PUTSEG: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 555 | sk_assert(u_in->tag1 == TempReg); |
sewardj | d077f53 | 2002-09-30 21:52:50 +0000 | [diff] [blame] | 556 | uInstr1(cb, TESTV, 2, TempReg, SHADOW(u_in->val1)); |
| 557 | uInstr1(cb, SETV, 2, TempReg, SHADOW(u_in->val1)); |
| 558 | VG_(copy_UInstr)(cb, u_in); |
| 559 | break; |
| 560 | |
| 561 | case GETSEG: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 562 | sk_assert(u_in->tag2 == TempReg); |
sewardj | d077f53 | 2002-09-30 21:52:50 +0000 | [diff] [blame] | 563 | uInstr1(cb, SETV, 2, TempReg, SHADOW(u_in->val2)); |
| 564 | VG_(copy_UInstr)(cb, u_in); |
| 565 | break; |
| 566 | |
| 567 | case USESEG: |
| 568 | VG_(copy_UInstr)(cb, u_in); |
| 569 | break; |
| 570 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 571 | /* Loads and stores. Test the V bits for the address. 24 |
| 572 | Mar 02: since the address is A-checked anyway, there's not |
| 573 | really much point in doing the V-check too, unless you |
| 574 | think that you might use addresses which are undefined but |
| 575 | still addressible. Hence the optionalisation of the V |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 576 | check. 15 Dec 02: optionalisation removed, since it no |
| 577 | longer makes much sense given we also have an addrcheck |
| 578 | skin. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 579 | |
| 580 | The LOADV/STOREV does an addressibility check for the |
| 581 | address. */ |
| 582 | |
| 583 | case LOAD: |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 584 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val1)); |
| 585 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 586 | uInstr2(cb, LOADV, u_in->size, |
| 587 | TempReg, u_in->val1, |
| 588 | TempReg, SHADOW(u_in->val2)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 589 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 590 | break; |
| 591 | |
| 592 | case STORE: |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 593 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val2)); |
| 594 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val2)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 595 | uInstr2(cb, STOREV, u_in->size, |
| 596 | TempReg, SHADOW(u_in->val1), |
| 597 | TempReg, u_in->val2); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 598 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 599 | break; |
| 600 | |
| 601 | /* Moving stuff around. Make the V bits follow accordingly, |
| 602 | but don't do anything else. */ |
| 603 | |
| 604 | case GET: |
| 605 | uInstr2(cb, GETV, u_in->size, |
| 606 | ArchReg, u_in->val1, |
| 607 | TempReg, SHADOW(u_in->val2)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 608 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 609 | break; |
| 610 | |
| 611 | case PUT: |
| 612 | uInstr2(cb, PUTV, u_in->size, |
| 613 | TempReg, SHADOW(u_in->val1), |
| 614 | ArchReg, u_in->val2); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 615 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 616 | break; |
| 617 | |
| 618 | case GETF: |
| 619 | /* This is not the smartest way to do it, but should work. */ |
| 620 | qd = create_GETVF(cb, u_in->size); |
| 621 | uInstr2(cb, MOV, 4, TempReg, qd, TempReg, SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 622 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | case PUTF: |
| 626 | create_PUTVF(cb, u_in->size, SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 627 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 628 | break; |
| 629 | |
| 630 | case MOV: |
| 631 | switch (u_in->tag1) { |
| 632 | case TempReg: |
| 633 | uInstr2(cb, MOV, 4, |
| 634 | TempReg, SHADOW(u_in->val1), |
| 635 | TempReg, SHADOW(u_in->val2)); |
| 636 | break; |
| 637 | case Literal: |
| 638 | uInstr1(cb, SETV, u_in->size, |
| 639 | TempReg, SHADOW(u_in->val2)); |
| 640 | break; |
| 641 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 642 | VG_(skin_panic)("memcheck_instrument: MOV"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 643 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 644 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 645 | break; |
| 646 | |
| 647 | /* Special case of add, where one of the operands is a literal. |
| 648 | lea1(t) = t + some literal. |
| 649 | Therefore: lea1#(qa) = left(qa) |
| 650 | */ |
| 651 | case LEA1: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 652 | sk_assert(u_in->size == 4 && !VG_(any_flag_use)(u_in)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 653 | qs = SHADOW(u_in->val1); |
| 654 | qd = SHADOW(u_in->val2); |
| 655 | uInstr2(cb, MOV, 4, TempReg, qs, TempReg, qd); |
| 656 | create_Left(cb, u_in->size, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 657 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | /* Another form of add. |
| 661 | lea2(ts,tt,shift) = ts + (tt << shift); shift is a literal |
| 662 | and is 0,1,2 or 3. |
| 663 | lea2#(qs,qt) = left(qs `UifU` (qt << shift)). |
| 664 | Note, subtly, that the shift puts zeroes at the bottom of qt, |
| 665 | meaning Valid, since the corresponding shift of tt puts |
| 666 | zeroes at the bottom of tb. |
| 667 | */ |
| 668 | case LEA2: { |
| 669 | Int shift; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 670 | sk_assert(u_in->size == 4 && !VG_(any_flag_use)(u_in)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 671 | switch (u_in->extra4b) { |
| 672 | case 1: shift = 0; break; |
| 673 | case 2: shift = 1; break; |
| 674 | case 4: shift = 2; break; |
| 675 | case 8: shift = 3; break; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 676 | default: VG_(skin_panic)( "memcheck_instrument(LEA2)" ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 677 | } |
| 678 | qs = SHADOW(u_in->val1); |
| 679 | qt = SHADOW(u_in->val2); |
| 680 | qd = SHADOW(u_in->val3); |
| 681 | uInstr2(cb, MOV, 4, TempReg, qt, TempReg, qd); |
| 682 | if (shift > 0) { |
| 683 | uInstr2(cb, SHL, 4, Literal, 0, TempReg, qd); |
| 684 | uLiteral(cb, shift); |
| 685 | } |
| 686 | create_UifU(cb, 4, qs, qd); |
| 687 | create_Left(cb, u_in->size, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 688 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | |
| 692 | /* inc#/dec#(qd) = q `UifU` left(qd) = left(qd) */ |
| 693 | case INC: case DEC: |
| 694 | qd = SHADOW(u_in->val1); |
| 695 | create_Left(cb, u_in->size, qd); |
| 696 | if (u_in->flags_w != FlagsEmpty) |
| 697 | create_PUTVF(cb, u_in->size, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 698 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | /* This is a HACK (approximation :-) */ |
| 702 | /* rcl#/rcr#(qs,qd) |
| 703 | = let q0 = pcast-sz-0(qd) `UifU` pcast-sz-0(qs) `UifU` eflags# |
| 704 | eflags# = q0 |
| 705 | qd =pcast-0-sz(q0) |
| 706 | Ie, cast everything down to a single bit, then back up. |
| 707 | This assumes that any bad bits infect the whole word and |
| 708 | the eflags. |
| 709 | */ |
| 710 | case RCL: case RCR: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 711 | sk_assert(u_in->flags_r != FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 712 | /* The following assertion looks like it makes sense, but is |
| 713 | actually wrong. Consider this: |
| 714 | rcll %eax |
| 715 | imull %eax, %eax |
| 716 | The rcll writes O and C but so does the imull, so the O and C |
| 717 | write of the rcll is annulled by the prior improvement pass. |
| 718 | Noticed by Kevin Ryde <user42@zip.com.au> |
| 719 | */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 720 | /* sk_assert(u_in->flags_w != FlagsEmpty); */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 721 | qs = getOperandShadow(cb, u_in->size, u_in->tag1, u_in->val1); |
| 722 | /* We can safely modify qs; cast it to 0-size. */ |
| 723 | create_PCast(cb, u_in->size, 0, qs); |
| 724 | qd = SHADOW(u_in->val2); |
| 725 | create_PCast(cb, u_in->size, 0, qd); |
| 726 | /* qs is cast-to-0(shift count#), and qd is cast-to-0(value#). */ |
| 727 | create_UifU(cb, 0, qs, qd); |
| 728 | /* qs is now free; reuse it for the flag definedness. */ |
| 729 | qs = create_GETVF(cb, 0); |
| 730 | create_UifU(cb, 0, qs, qd); |
| 731 | create_PUTVF(cb, 0, qd); |
| 732 | create_PCast(cb, 0, u_in->size, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 733 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 734 | break; |
| 735 | |
| 736 | /* for OP in shl shr sar rol ror |
| 737 | (qs is shift count#, qd is value to be OP#d) |
| 738 | OP(ts,td) |
| 739 | OP#(qs,qd) |
| 740 | = pcast-1-sz(qs) `UifU` OP(ts,qd) |
| 741 | So we apply OP to the tag bits too, and then UifU with |
| 742 | the shift count# to take account of the possibility of it |
| 743 | being undefined. |
| 744 | |
| 745 | A bit subtle: |
| 746 | ROL/ROR rearrange the tag bits as per the value bits. |
| 747 | SHL/SHR shifts zeroes into the value, and corresponding |
| 748 | zeroes indicating Definedness into the tag. |
| 749 | SAR copies the top bit of the value downwards, and therefore |
| 750 | SAR also copies the definedness of the top bit too. |
| 751 | So in all five cases, we just apply the same op to the tag |
| 752 | bits as is applied to the value bits. Neat! |
| 753 | */ |
| 754 | case SHL: |
| 755 | case SHR: case SAR: |
| 756 | case ROL: case ROR: { |
| 757 | Int t_amount = INVALID_TEMPREG; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 758 | sk_assert(u_in->tag1 == TempReg || u_in->tag1 == Literal); |
| 759 | sk_assert(u_in->tag2 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 760 | qd = SHADOW(u_in->val2); |
| 761 | |
| 762 | /* Make qs hold shift-count# and make |
| 763 | t_amount be a TempReg holding the shift count. */ |
| 764 | if (u_in->tag1 == Literal) { |
| 765 | t_amount = newTemp(cb); |
| 766 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_amount); |
| 767 | uLiteral(cb, u_in->lit32); |
| 768 | qs = SHADOW(t_amount); |
| 769 | uInstr1(cb, SETV, 1, TempReg, qs); |
| 770 | } else { |
| 771 | t_amount = u_in->val1; |
| 772 | qs = SHADOW(u_in->val1); |
| 773 | } |
| 774 | |
| 775 | uInstr2(cb, u_in->opcode, |
| 776 | u_in->size, |
| 777 | TempReg, t_amount, |
| 778 | TempReg, qd); |
| 779 | qt = newShadow(cb); |
| 780 | uInstr2(cb, MOV, 4, TempReg, qs, TempReg, qt); |
| 781 | create_PCast(cb, 1, u_in->size, qt); |
| 782 | create_UifU(cb, u_in->size, qt, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 783 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | |
| 787 | /* One simple tag operation. */ |
| 788 | case WIDEN: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 789 | sk_assert(u_in->tag1 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 790 | create_Widen(cb, u_in->signed_widen, u_in->extra4b, u_in->size, |
| 791 | SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 792 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 793 | break; |
| 794 | |
| 795 | /* not#(x) = x (since bitwise independent) */ |
| 796 | case NOT: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 797 | sk_assert(u_in->tag1 == TempReg); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 798 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 799 | break; |
| 800 | |
| 801 | /* neg#(x) = left(x) (derivable from case for SUB) */ |
| 802 | case NEG: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 803 | sk_assert(u_in->tag1 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 804 | create_Left(cb, u_in->size, SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 805 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 806 | break; |
| 807 | |
| 808 | /* bswap#(x) = bswap(x) */ |
| 809 | case BSWAP: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 810 | sk_assert(u_in->tag1 == TempReg); |
| 811 | sk_assert(u_in->size == 4); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 812 | qd = SHADOW(u_in->val1); |
| 813 | uInstr1(cb, BSWAP, 4, TempReg, qd); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 814 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 815 | break; |
| 816 | |
| 817 | /* cc2val#(qd) = pcast-0-to-size(eflags#) */ |
| 818 | case CC2VAL: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 819 | sk_assert(u_in->tag1 == TempReg); |
| 820 | sk_assert(u_in->flags_r != FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 821 | qt = create_GETVF(cb, u_in->size); |
| 822 | uInstr2(cb, MOV, 4, TempReg, qt, TempReg, SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 823 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 824 | break; |
| 825 | |
| 826 | /* cmov#(qs,qd) = cmov(qs,qd) |
| 827 | That is, do the cmov of tags using the same flags as for |
| 828 | the data (obviously). However, first do a test on the |
| 829 | validity of the flags. |
| 830 | */ |
| 831 | case CMOV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 832 | sk_assert(u_in->size == 4); |
| 833 | sk_assert(u_in->tag1 == TempReg); |
| 834 | sk_assert(u_in->tag2 == TempReg); |
| 835 | sk_assert(u_in->flags_r != FlagsEmpty); |
| 836 | sk_assert(u_in->flags_w == FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 837 | qs = SHADOW(u_in->val1); |
| 838 | qd = SHADOW(u_in->val2); |
| 839 | qt = create_GETVF(cb, 0); |
| 840 | uInstr1(cb, TESTV, 0, TempReg, qt); |
| 841 | /* qt should never be referred to again. Nevertheless |
| 842 | ... */ |
| 843 | uInstr1(cb, SETV, 0, TempReg, qt); |
| 844 | |
| 845 | uInstr2(cb, CMOV, 4, TempReg, qs, TempReg, qd); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 846 | uCond(cb, u_in->cond); |
| 847 | uFlagsRWU(cb, u_in->flags_r, u_in->flags_w, FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 848 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 849 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 850 | break; |
| 851 | |
| 852 | /* add#/sub#(qs,qd) |
| 853 | = qs `UifU` qd `UifU` left(qs) `UifU` left(qd) |
| 854 | = left(qs) `UifU` left(qd) |
| 855 | = left(qs `UifU` qd) |
| 856 | adc#/sbb#(qs,qd) |
| 857 | = left(qs `UifU` qd) `UifU` pcast(eflags#) |
| 858 | Second arg (dest) is TempReg. |
| 859 | First arg (src) is Literal or TempReg or ArchReg. |
| 860 | */ |
| 861 | case ADD: case SUB: |
| 862 | case ADC: case SBB: |
jsgf | 5efa4fd | 2003-10-14 21:49:11 +0000 | [diff] [blame] | 863 | case MUL: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 864 | qd = SHADOW(u_in->val2); |
| 865 | qs = getOperandShadow(cb, u_in->size, u_in->tag1, u_in->val1); |
| 866 | create_UifU(cb, u_in->size, qs, qd); |
| 867 | create_Left(cb, u_in->size, qd); |
| 868 | if (u_in->opcode == ADC || u_in->opcode == SBB) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 869 | sk_assert(u_in->flags_r != FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 870 | qt = create_GETVF(cb, u_in->size); |
| 871 | create_UifU(cb, u_in->size, qt, qd); |
| 872 | } |
| 873 | if (u_in->flags_w != FlagsEmpty) { |
| 874 | create_PUTVF(cb, u_in->size, qd); |
| 875 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 876 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 877 | break; |
| 878 | |
| 879 | /* xor#(qs,qd) = qs `UifU` qd */ |
| 880 | case XOR: |
| 881 | qd = SHADOW(u_in->val2); |
| 882 | qs = getOperandShadow(cb, u_in->size, u_in->tag1, u_in->val1); |
| 883 | create_UifU(cb, u_in->size, qs, qd); |
| 884 | if (u_in->flags_w != FlagsEmpty) { |
| 885 | create_PUTVF(cb, u_in->size, qd); |
| 886 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 887 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 888 | break; |
| 889 | |
| 890 | /* and#/or#(qs,qd) |
| 891 | = (qs `UifU` qd) `DifD` improve(vs,qs) |
| 892 | `DifD` improve(vd,qd) |
| 893 | where improve is the relevant one of |
| 894 | Improve{AND,OR}_TQ |
| 895 | Use the following steps, with qt as a temp: |
| 896 | qt = improve(vd,qd) |
| 897 | qd = qs `UifU` qd |
| 898 | qd = qt `DifD` qd |
| 899 | qt = improve(vs,qs) |
| 900 | qd = qt `DifD` qd |
| 901 | */ |
| 902 | case AND: case OR: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 903 | sk_assert(u_in->tag1 == TempReg); |
| 904 | sk_assert(u_in->tag2 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 905 | qd = SHADOW(u_in->val2); |
| 906 | qs = SHADOW(u_in->val1); |
| 907 | qt = newShadow(cb); |
| 908 | |
| 909 | /* qt = improve(vd,qd) */ |
| 910 | uInstr2(cb, MOV, 4, TempReg, qd, TempReg, qt); |
| 911 | if (u_in->opcode == AND) |
| 912 | create_ImproveAND_TQ(cb, u_in->size, u_in->val2, qt); |
| 913 | else |
| 914 | create_ImproveOR_TQ(cb, u_in->size, u_in->val2, qt); |
| 915 | /* qd = qs `UifU` qd */ |
| 916 | create_UifU(cb, u_in->size, qs, qd); |
| 917 | /* qd = qt `DifD` qd */ |
| 918 | create_DifD(cb, u_in->size, qt, qd); |
| 919 | /* qt = improve(vs,qs) */ |
| 920 | uInstr2(cb, MOV, 4, TempReg, qs, TempReg, qt); |
| 921 | if (u_in->opcode == AND) |
| 922 | create_ImproveAND_TQ(cb, u_in->size, u_in->val1, qt); |
| 923 | else |
| 924 | create_ImproveOR_TQ(cb, u_in->size, u_in->val1, qt); |
| 925 | /* qd = qt `DifD` qd */ |
| 926 | create_DifD(cb, u_in->size, qt, qd); |
| 927 | /* So, finally qd is the result tag. */ |
| 928 | if (u_in->flags_w != FlagsEmpty) { |
| 929 | create_PUTVF(cb, u_in->size, qd); |
| 930 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 931 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 932 | break; |
| 933 | |
| 934 | /* Machinery to do with supporting CALLM. Copy the start and |
| 935 | end markers only to make the result easier to read |
| 936 | (debug); they generate no code and have no effect. |
| 937 | */ |
| 938 | case CALLM_S: case CALLM_E: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 939 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 940 | break; |
| 941 | |
| 942 | /* Copy PUSH and POP verbatim. Arg/result absval |
| 943 | calculations are done when the associated CALL is |
| 944 | processed. CLEAR has no effect on absval calculations but |
| 945 | needs to be copied. |
| 946 | */ |
| 947 | case PUSH: case POP: case CLEAR: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 948 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 949 | break; |
| 950 | |
| 951 | /* In short: |
| 952 | callm#(a1# ... an#) = (a1# `UifU` ... `UifU` an#) |
| 953 | We have to decide on a size to do the computation at, |
| 954 | although the choice doesn't affect correctness. We will |
| 955 | do a pcast to the final size anyway, so the only important |
| 956 | factor is to choose a size which minimises the total |
| 957 | number of casts needed. Valgrind: just use size 0, |
| 958 | regardless. It may not be very good for performance |
| 959 | but does simplify matters, mainly by reducing the number |
| 960 | of different pessimising casts which have to be implemented. |
| 961 | */ |
| 962 | case CALLM: { |
| 963 | UInstr* uu; |
| 964 | Bool res_used; |
| 965 | |
| 966 | /* Now generate the code. Get the final result absval |
| 967 | into qt. */ |
| 968 | qt = newShadow(cb); |
| 969 | qtt = newShadow(cb); |
| 970 | uInstr1(cb, SETV, 0, TempReg, qt); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 971 | for (j = i-1; VG_(get_instr)(cb_in, j)->opcode != CALLM_S; j--) { |
| 972 | uu = VG_(get_instr)(cb_in, j); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 973 | if (uu->opcode != PUSH) continue; |
| 974 | /* cast via a temporary */ |
| 975 | uInstr2(cb, MOV, 4, TempReg, SHADOW(uu->val1), |
| 976 | TempReg, qtt); |
| 977 | create_PCast(cb, uu->size, 0, qtt); |
| 978 | create_UifU(cb, 0, qtt, qt); |
| 979 | } |
| 980 | /* Remembering also that flags read count as inputs. */ |
| 981 | if (u_in->flags_r != FlagsEmpty) { |
| 982 | qtt = create_GETVF(cb, 0); |
| 983 | create_UifU(cb, 0, qtt, qt); |
| 984 | } |
| 985 | |
| 986 | /* qt now holds the result tag. If any results from the |
| 987 | call are used, either by fetching with POP or |
| 988 | implicitly by writing the flags, we copy the result |
| 989 | absval to the relevant location. If not used, the call |
| 990 | must have been for its side effects, so we test qt here |
| 991 | and now. Note that this assumes that all values |
| 992 | removed by POP continue to be live. So dead args |
| 993 | *must* be removed with CLEAR, not by POPping them into |
| 994 | a dummy tempreg. |
| 995 | */ |
| 996 | res_used = False; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 997 | for (j = i+1; VG_(get_instr)(cb_in, j)->opcode != CALLM_E; j++) { |
| 998 | uu = VG_(get_instr)(cb_in, j); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 999 | if (uu->opcode != POP) continue; |
| 1000 | /* Cast via a temp. */ |
| 1001 | uInstr2(cb, MOV, 4, TempReg, qt, TempReg, qtt); |
| 1002 | create_PCast(cb, 0, uu->size, qtt); |
| 1003 | uInstr2(cb, MOV, 4, TempReg, qtt, |
| 1004 | TempReg, SHADOW(uu->val1)); |
| 1005 | res_used = True; |
| 1006 | } |
| 1007 | if (u_in->flags_w != FlagsEmpty) { |
| 1008 | create_PUTVF(cb, 0, qt); |
| 1009 | res_used = True; |
| 1010 | } |
| 1011 | if (!res_used) { |
| 1012 | uInstr1(cb, TESTV, 0, TempReg, qt); |
| 1013 | /* qt should never be referred to again. Nevertheless |
| 1014 | ... */ |
| 1015 | uInstr1(cb, SETV, 0, TempReg, qt); |
| 1016 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1017 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1018 | break; |
| 1019 | } |
| 1020 | /* Whew ... */ |
| 1021 | |
| 1022 | case JMP: |
| 1023 | if (u_in->tag1 == TempReg) { |
| 1024 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val1)); |
| 1025 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val1)); |
| 1026 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1027 | sk_assert(u_in->tag1 == Literal); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1028 | } |
| 1029 | if (u_in->cond != CondAlways) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1030 | sk_assert(u_in->flags_r != FlagsEmpty); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1031 | qt = create_GETVF(cb, 0); |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 1032 | if (/* HACK */ bogusLiterals) { |
| 1033 | if (0) |
| 1034 | VG_(printf)("ignore TESTV due to bogus literal\n"); |
| 1035 | } else { |
| 1036 | uInstr1(cb, TESTV, 0, TempReg, qt); |
| 1037 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1038 | /* qt should never be referred to again. Nevertheless |
| 1039 | ... */ |
| 1040 | uInstr1(cb, SETV, 0, TempReg, qt); |
| 1041 | } |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1042 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | |
| 1045 | case JIFZ: |
| 1046 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val1)); |
| 1047 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val1)); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1048 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | |
| 1051 | /* Emit a check on the address used. The value loaded into the |
| 1052 | FPU is checked by the call to fpu_{read/write}_check(). */ |
sewardj | 901a4a9 | 2003-03-30 01:52:26 +0000 | [diff] [blame] | 1053 | case MMX2_MemRd: case MMX2_MemWr: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1054 | case FPU_R: case FPU_W: { |
| 1055 | Int t_size = INVALID_TEMPREG; |
njn | fa509e2 | 2003-08-28 17:46:16 +0000 | [diff] [blame] | 1056 | Bool is_load; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1057 | |
sewardj | d797101 | 2003-04-04 00:21:58 +0000 | [diff] [blame] | 1058 | if (u_in->opcode == MMX2_MemRd || u_in->opcode == MMX2_MemWr) |
| 1059 | sk_assert(u_in->size == 4 || u_in->size == 8); |
| 1060 | |
njn | fa509e2 | 2003-08-28 17:46:16 +0000 | [diff] [blame] | 1061 | is_load = u_in->opcode==FPU_R || u_in->opcode==MMX2_MemRd; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1062 | sk_assert(u_in->tag2 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1063 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val2)); |
| 1064 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val2)); |
| 1065 | |
| 1066 | t_size = newTemp(cb); |
| 1067 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_size); |
| 1068 | uLiteral(cb, u_in->size); |
| 1069 | uInstr2(cb, CCALL, 0, TempReg, u_in->val2, TempReg, t_size); |
njn | fa509e2 | 2003-08-28 17:46:16 +0000 | [diff] [blame] | 1070 | uCCall(cb, is_load ? (Addr) & MC_(fpu_read_check) |
| 1071 | : (Addr) & MC_(fpu_write_check), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1072 | 2, 2, False); |
| 1073 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1074 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1078 | /* SSE ins referencing scalar integer registers */ |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1079 | case SSE3g_RegWr: |
| 1080 | case SSE3e_RegRd: |
| 1081 | case SSE3e_RegWr: |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1082 | case SSE3g1_RegWr: |
| 1083 | case SSE3e1_RegRd: |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1084 | sk_assert(u_in->tag3 == TempReg); |
| 1085 | |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1086 | if (u_in->opcode == SSE3e1_RegRd) { |
| 1087 | sk_assert(u_in->size == 2); |
| 1088 | } else { |
| 1089 | sk_assert(u_in->size == 4); |
| 1090 | } |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1091 | |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1092 | /* Is it a read ? Better check the V bits right now. */ |
| 1093 | if ( u_in->opcode == SSE3e_RegRd |
| 1094 | || u_in->opcode == SSE3e1_RegRd ) |
| 1095 | uInstr1(cb, TESTV, u_in->size, |
| 1096 | TempReg, SHADOW(u_in->val3)); |
| 1097 | |
| 1098 | /* And for both read and write, set the register to be |
| 1099 | defined. */ |
| 1100 | uInstr1(cb, SETV, u_in->size, |
| 1101 | TempReg, SHADOW(u_in->val3)); |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1102 | |
| 1103 | VG_(copy_UInstr)(cb, u_in); |
| 1104 | break; |
| 1105 | |
| 1106 | /* ... and the same deal for SSE insns referencing memory */ |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1107 | case SSE3a_MemRd: |
| 1108 | case SSE3a_MemWr: |
| 1109 | case SSE2a_MemWr: |
sewardj | 77d30a2 | 2003-10-19 08:18:52 +0000 | [diff] [blame] | 1110 | case SSE2a_MemRd: |
| 1111 | case SSE3a1_MemRd: { |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1112 | Bool is_load; |
| 1113 | Int t_size; |
| 1114 | |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 1115 | sk_assert(u_in->size == 4 || u_in->size == 8 |
| 1116 | || u_in->size == 16 || u_in->size == 512); |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1117 | |
| 1118 | t_size = INVALID_TEMPREG; |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1119 | is_load = u_in->opcode==SSE2a_MemRd |
sewardj | 77d30a2 | 2003-10-19 08:18:52 +0000 | [diff] [blame] | 1120 | || u_in->opcode==SSE3a_MemRd |
| 1121 | || u_in->opcode==SSE3a1_MemRd; |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1122 | |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1123 | sk_assert(u_in->tag3 == TempReg); |
| 1124 | |
| 1125 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val3)); |
| 1126 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val3)); |
| 1127 | t_size = newTemp(cb); |
| 1128 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_size); |
| 1129 | uLiteral(cb, u_in->size); |
| 1130 | uInstr2(cb, CCALL, 0, TempReg, u_in->val3, TempReg, t_size); |
| 1131 | uCCall(cb, is_load ? (Addr) & MC_(fpu_read_check) |
| 1132 | : (Addr) & MC_(fpu_write_check), |
| 1133 | 2, 2, False); |
| 1134 | |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1135 | VG_(copy_UInstr)(cb, u_in); |
| 1136 | break; |
| 1137 | } |
sewardj | b111685 | 2003-10-17 21:51:28 +0000 | [diff] [blame] | 1138 | |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1139 | case SSE3ag_MemRd_RegWr: |
| 1140 | { |
| 1141 | Int t_size; |
sewardj | 93992e2 | 2003-05-26 09:17:41 +0000 | [diff] [blame] | 1142 | |
daywalker | b6d9b54 | 2003-09-26 00:16:25 +0000 | [diff] [blame] | 1143 | sk_assert(u_in->size == 4 || u_in->size == 8); |
| 1144 | sk_assert(u_in->tag1 == TempReg); |
| 1145 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val1)); |
| 1146 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val1)); |
| 1147 | t_size = newTemp(cb); |
| 1148 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_size); |
| 1149 | uLiteral(cb, u_in->size); |
| 1150 | uInstr2(cb, CCALL, 0, TempReg, u_in->val1, TempReg, t_size); |
| 1151 | uCCall(cb, (Addr) MC_(fpu_read_check), 2, 2, False ); |
| 1152 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val2)); |
| 1153 | VG_(copy_UInstr)(cb, u_in); |
| 1154 | break; |
| 1155 | } |
sewardj | 247f32f | 2003-10-03 20:40:45 +0000 | [diff] [blame] | 1156 | |
| 1157 | /* For FPU, MMX and SSE insns not referencing memory, just |
| 1158 | copy thru. */ |
| 1159 | case SSE5: case SSE4: case SSE3: |
sewardj | 901a4a9 | 2003-03-30 01:52:26 +0000 | [diff] [blame] | 1160 | case MMX1: case MMX2: case MMX3: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1161 | case FPU: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1162 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | |
sewardj | 901a4a9 | 2003-03-30 01:52:26 +0000 | [diff] [blame] | 1165 | /* Since we don't track definedness of values inside the |
| 1166 | MMX state, we'd better check that the (int) reg being |
| 1167 | read here is defined. */ |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 1168 | case MMX2_ERegRd: |
sewardj | 901a4a9 | 2003-03-30 01:52:26 +0000 | [diff] [blame] | 1169 | sk_assert(u_in->tag2 == TempReg); |
| 1170 | sk_assert(u_in->size == 4); |
| 1171 | uInstr1(cb, TESTV, 4, TempReg, SHADOW(u_in->val2)); |
| 1172 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val2)); |
| 1173 | VG_(copy_UInstr)(cb, u_in); |
| 1174 | break; |
| 1175 | |
sewardj | d1c9e43 | 2003-04-04 20:40:34 +0000 | [diff] [blame] | 1176 | /* The MMX register is assumed to be fully defined, so |
| 1177 | that's what this register becomes. */ |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 1178 | case MMX2_ERegWr: |
sewardj | d1c9e43 | 2003-04-04 20:40:34 +0000 | [diff] [blame] | 1179 | sk_assert(u_in->tag2 == TempReg); |
| 1180 | sk_assert(u_in->size == 4); |
| 1181 | uInstr1(cb, SETV, 4, TempReg, SHADOW(u_in->val2)); |
| 1182 | VG_(copy_UInstr)(cb, u_in); |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1185 | default: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1186 | VG_(pp_UInstr)(0, u_in); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1187 | VG_(skin_panic)( "memcheck_instrument: unhandled case"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1188 | |
| 1189 | } /* end of switch (u_in->opcode) */ |
| 1190 | |
| 1191 | } /* end of for loop */ |
| 1192 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1193 | VG_(free_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1194 | return cb; |
| 1195 | } |
| 1196 | |
| 1197 | /*------------------------------------------------------------*/ |
| 1198 | /*--- Clean up mem check instrumentation. ---*/ |
| 1199 | /*------------------------------------------------------------*/ |
| 1200 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1201 | #define dis VG_(print_codegen) |
| 1202 | |
| 1203 | |
| 1204 | #define VGC_IS_SHADOW(tempreg) ((tempreg % 2) == 1) |
| 1205 | #define VGC_UNDEF ((UChar)100) |
| 1206 | #define VGC_VALUE ((UChar)101) |
| 1207 | |
| 1208 | #define NOP_no_msg(uu) \ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1209 | do { VG_(new_NOP)(uu); } while (False) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1210 | |
| 1211 | #define NOP_tag1_op(uu) \ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1212 | do { VG_(new_NOP)(uu); \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1213 | if (dis) \ |
| 1214 | VG_(printf)(" at %2d: delete %s due to defd arg\n", \ |
| 1215 | i, nameOfTagOp(u->val3)); \ |
| 1216 | } while (False) |
| 1217 | |
| 1218 | #define SETV_tag1_op(uu,newsz) \ |
| 1219 | do { uu->opcode = SETV; \ |
| 1220 | uu->size = newsz; \ |
| 1221 | uu->tag2 = uu->tag3 = NoValue; \ |
| 1222 | if (dis) \ |
| 1223 | VG_(printf)(" at %2d: convert %s to SETV%d " \ |
| 1224 | "due to defd arg\n", \ |
| 1225 | i, nameOfTagOp(u->val3), newsz); \ |
| 1226 | } while (False) |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | /* Run backwards and delete SETVs on shadow temps for which the next |
| 1231 | action is a write. Needs an env saying whether or not the next |
| 1232 | action is a write. The supplied UCodeBlock is destructively |
| 1233 | modified. |
| 1234 | */ |
| 1235 | static void vg_delete_redundant_SETVs ( UCodeBlock* cb ) |
| 1236 | { |
| 1237 | Int i, j, k; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1238 | Int n_temps = VG_(get_num_temps)(cb); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1239 | Bool* next_is_write; |
| 1240 | UInstr* u; |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1241 | Int tempUse[VG_MAX_REGS_USED]; |
| 1242 | Bool isWrites[VG_MAX_REGS_USED]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (n_temps == 0) return; |
| 1245 | |
| 1246 | next_is_write = VG_(malloc)(n_temps * sizeof(Bool)); |
| 1247 | |
| 1248 | for (i = 0; i < n_temps; i++) next_is_write[i] = True; |
| 1249 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1250 | for (i = VG_(get_num_instrs)(cb) - 1; i >= 0; i--) { |
| 1251 | u = VG_(get_instr)(cb, i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1252 | |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 1253 | /* Occasionally there will be GETVs, TAG1s and TAG2s calculating |
| 1254 | values which are never used. These first three cases get rid |
| 1255 | of them. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (u->opcode == GETV && VGC_IS_SHADOW(u->val2) |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 1258 | && next_is_write[u->val2]) { |
| 1259 | sk_assert(u->val2 < n_temps); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1260 | VG_(new_NOP)(u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1261 | if (dis) |
| 1262 | VG_(printf)(" at %2d: delete GETV\n", i); |
| 1263 | } else |
| 1264 | |
| 1265 | if (u->opcode == TAG1 && VGC_IS_SHADOW(u->val1) |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 1266 | && next_is_write[u->val1]) { |
| 1267 | sk_assert(u->val1 < n_temps); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1268 | VG_(new_NOP)(u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1269 | if (dis) |
| 1270 | VG_(printf)(" at %2d: delete TAG1\n", i); |
| 1271 | } else |
| 1272 | |
| 1273 | if (u->opcode == TAG2 && VGC_IS_SHADOW(u->val2) |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 1274 | && next_is_write[u->val2]) { |
| 1275 | sk_assert(u->val2 < n_temps); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1276 | VG_(new_NOP)(u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1277 | if (dis) |
| 1278 | VG_(printf)(" at %2d: delete TAG2\n", i); |
| 1279 | } else |
| 1280 | |
sewardj | 5164748 | 2002-12-15 01:42:22 +0000 | [diff] [blame] | 1281 | /* The bulk of the cleanup work of this function is done by |
| 1282 | the code from here downwards. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1283 | |
| 1284 | if (u->opcode == MOV && VGC_IS_SHADOW(u->val2) |
| 1285 | && next_is_write[u->val2]) { |
| 1286 | /* This MOV is pointless because the target is dead at this |
| 1287 | point. Delete it. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1288 | VG_(new_NOP)(u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1289 | if (dis) |
| 1290 | VG_(printf)(" at %2d: delete MOV\n", i); |
| 1291 | } else |
| 1292 | |
| 1293 | if (u->opcode == SETV) { |
| 1294 | if (u->tag1 == TempReg) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1295 | sk_assert(VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1296 | if (next_is_write[u->val1]) { |
| 1297 | /* This write is pointless, so annul it. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1298 | VG_(new_NOP)(u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1299 | if (dis) |
| 1300 | VG_(printf)(" at %2d: delete SETV\n", i); |
| 1301 | } else { |
| 1302 | /* This write has a purpose; don't annul it, but do |
| 1303 | notice that we did it. */ |
| 1304 | next_is_write[u->val1] = True; |
| 1305 | } |
| 1306 | |
| 1307 | } |
| 1308 | |
| 1309 | } else { |
| 1310 | /* Find out what this insn does to the temps. */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1311 | k = VG_(get_reg_usage)(u, TempReg, &tempUse[0], &isWrites[0]); |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1312 | sk_assert(0 <= k && k <= VG_MAX_REGS_USED); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1313 | for (j = k-1; j >= 0; j--) { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1314 | next_is_write[ tempUse[j] ] = isWrites[j]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | /* Run forwards, propagating and using the is-completely-defined |
| 1322 | property. This removes a lot of redundant tag-munging code. |
| 1323 | Unfortunately it requires intimate knowledge of how each uinstr and |
| 1324 | tagop modifies its arguments. This duplicates knowledge of uinstr |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1325 | tempreg uses embodied in VG_(get_reg_usage)(), which is unfortunate. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1326 | The supplied UCodeBlock* is modified in-place. |
| 1327 | |
| 1328 | For each value temp, def[] should hold VGC_VALUE. |
| 1329 | |
| 1330 | For each shadow temp, def[] may hold 4,2,1 or 0 iff that shadow is |
| 1331 | definitely known to be fully defined at that size. In all other |
| 1332 | circumstances a shadow's def[] entry is VGC_UNDEF, meaning possibly |
| 1333 | undefined. In cases of doubt, VGC_UNDEF is always safe. |
| 1334 | */ |
| 1335 | static void vg_propagate_definedness ( UCodeBlock* cb ) |
| 1336 | { |
| 1337 | Int i, j, k, t; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1338 | Int n_temps = VG_(get_num_temps)(cb); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1339 | UChar* def; |
| 1340 | UInstr* u; |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1341 | Int tempUse[VG_MAX_REGS_USED]; |
| 1342 | Bool isWrites[VG_MAX_REGS_USED]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (n_temps == 0) return; |
| 1345 | |
| 1346 | def = VG_(malloc)(n_temps * sizeof(UChar)); |
| 1347 | |
| 1348 | for (i = 0; i < n_temps; i++) |
| 1349 | def[i] = VGC_IS_SHADOW(i) ? VGC_UNDEF : VGC_VALUE; |
| 1350 | |
| 1351 | /* Run forwards, detecting and using the all-defined property. */ |
| 1352 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1353 | for (i = 0; i < VG_(get_num_instrs)(cb); i++) { |
| 1354 | u = VG_(get_instr)(cb, i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1355 | switch (u->opcode) { |
| 1356 | |
| 1357 | /* Tag-handling uinstrs. */ |
| 1358 | |
| 1359 | /* Deal with these quickly. */ |
| 1360 | case NOP: |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 1361 | case LOCK: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1362 | case INCEIP: |
| 1363 | break; |
| 1364 | |
| 1365 | /* Make a tag defined. */ |
| 1366 | case SETV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1367 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1368 | def[u->val1] = u->size; |
| 1369 | break; |
| 1370 | |
| 1371 | /* Check definedness of a tag. */ |
| 1372 | case TESTV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1373 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1374 | if (def[u->val1] <= 4) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1375 | sk_assert(def[u->val1] == u->size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1376 | NOP_no_msg(u); |
| 1377 | if (dis) |
| 1378 | VG_(printf)(" at %2d: delete TESTV on defd arg\n", i); |
| 1379 | } |
| 1380 | break; |
| 1381 | |
| 1382 | /* Applies to both values and tags. Propagate Definedness |
| 1383 | property through copies. Note that this isn't optional; |
| 1384 | we *have* to do this to keep def[] correct. */ |
| 1385 | case MOV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1386 | sk_assert(u->tag2 == TempReg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1387 | if (u->tag1 == TempReg) { |
| 1388 | if (VGC_IS_SHADOW(u->val1)) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1389 | sk_assert(VGC_IS_SHADOW(u->val2)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1390 | def[u->val2] = def[u->val1]; |
| 1391 | } |
| 1392 | } |
| 1393 | break; |
| 1394 | |
| 1395 | case PUTV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1396 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1397 | if (def[u->val1] <= 4) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1398 | sk_assert(def[u->val1] == u->size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1399 | u->tag1 = Literal; |
| 1400 | u->val1 = 0; |
| 1401 | switch (u->size) { |
| 1402 | case 4: u->lit32 = 0x00000000; break; |
| 1403 | case 2: u->lit32 = 0xFFFF0000; break; |
| 1404 | case 1: u->lit32 = 0xFFFFFF00; break; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1405 | default: VG_(skin_panic)("vg_cleanup(PUTV)"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1406 | } |
| 1407 | if (dis) |
| 1408 | VG_(printf)( |
| 1409 | " at %2d: propagate definedness into PUTV\n", i); |
| 1410 | } |
| 1411 | break; |
| 1412 | |
| 1413 | case STOREV: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1414 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1415 | if (def[u->val1] <= 4) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1416 | sk_assert(def[u->val1] == u->size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1417 | u->tag1 = Literal; |
| 1418 | u->val1 = 0; |
| 1419 | switch (u->size) { |
| 1420 | case 4: u->lit32 = 0x00000000; break; |
| 1421 | case 2: u->lit32 = 0xFFFF0000; break; |
| 1422 | case 1: u->lit32 = 0xFFFFFF00; break; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1423 | default: VG_(skin_panic)("vg_cleanup(STOREV)"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1424 | } |
| 1425 | if (dis) |
| 1426 | VG_(printf)( |
| 1427 | " at %2d: propagate definedness into STandV\n", i); |
| 1428 | } |
| 1429 | break; |
| 1430 | |
| 1431 | /* Nothing interesting we can do with this, I think. */ |
| 1432 | case PUTVF: |
| 1433 | break; |
| 1434 | |
| 1435 | /* Tag handling operations. */ |
| 1436 | case TAG2: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1437 | sk_assert(u->tag2 == TempReg && VGC_IS_SHADOW(u->val2)); |
| 1438 | sk_assert(u->tag3 == Lit16); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1439 | /* Ultra-paranoid "type" checking. */ |
| 1440 | switch (u->val3) { |
| 1441 | case Tag_ImproveAND4_TQ: case Tag_ImproveAND2_TQ: |
| 1442 | case Tag_ImproveAND1_TQ: case Tag_ImproveOR4_TQ: |
| 1443 | case Tag_ImproveOR2_TQ: case Tag_ImproveOR1_TQ: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1444 | sk_assert(u->tag1 == TempReg && !VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1445 | break; |
| 1446 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1447 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | switch (u->val3) { |
| 1451 | Int sz; |
| 1452 | case Tag_UifU4: |
| 1453 | sz = 4; goto do_UifU; |
| 1454 | case Tag_UifU2: |
| 1455 | sz = 2; goto do_UifU; |
| 1456 | case Tag_UifU1: |
| 1457 | sz = 1; goto do_UifU; |
| 1458 | case Tag_UifU0: |
| 1459 | sz = 0; goto do_UifU; |
| 1460 | do_UifU: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1461 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
| 1462 | sk_assert(u->tag2 == TempReg && VGC_IS_SHADOW(u->val2)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1463 | if (def[u->val1] <= 4) { |
| 1464 | /* UifU. The first arg is defined, so result is |
| 1465 | simply second arg. Delete this operation. */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1466 | sk_assert(def[u->val1] == sz); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1467 | NOP_no_msg(u); |
| 1468 | if (dis) |
| 1469 | VG_(printf)( |
| 1470 | " at %2d: delete UifU%d due to defd arg1\n", |
| 1471 | i, sz); |
| 1472 | } |
| 1473 | else |
| 1474 | if (def[u->val2] <= 4) { |
| 1475 | /* UifU. The second arg is defined, so result is |
| 1476 | simply first arg. Copy to second. */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1477 | sk_assert(def[u->val2] == sz); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1478 | u->opcode = MOV; |
| 1479 | u->size = 4; |
| 1480 | u->tag3 = NoValue; |
| 1481 | def[u->val2] = def[u->val1]; |
| 1482 | if (dis) |
| 1483 | VG_(printf)( |
| 1484 | " at %2d: change UifU%d to MOV due to defd" |
| 1485 | " arg2\n", |
| 1486 | i, sz); |
| 1487 | } |
| 1488 | break; |
| 1489 | case Tag_ImproveAND4_TQ: |
| 1490 | sz = 4; goto do_ImproveAND; |
| 1491 | case Tag_ImproveAND1_TQ: |
| 1492 | sz = 1; goto do_ImproveAND; |
| 1493 | do_ImproveAND: |
mueller | 00a8a6f | 2004-01-02 22:46:34 +0000 | [diff] [blame] | 1494 | /* Implements Q = T AND Q. So if Q is entirely defined, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1495 | ie all 0s, we get MOV T, Q. */ |
| 1496 | if (def[u->val2] <= 4) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1497 | sk_assert(def[u->val2] == sz); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1498 | u->size = 4; /* Regardless of sz */ |
| 1499 | u->opcode = MOV; |
| 1500 | u->tag3 = NoValue; |
| 1501 | def[u->val2] = VGC_UNDEF; |
| 1502 | if (dis) |
| 1503 | VG_(printf)( |
| 1504 | " at %2d: change ImproveAND%d_TQ to MOV due " |
| 1505 | "to defd arg2\n", |
| 1506 | i, sz); |
| 1507 | } |
| 1508 | break; |
| 1509 | default: |
| 1510 | goto unhandled; |
| 1511 | } |
| 1512 | break; |
| 1513 | |
| 1514 | case TAG1: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1515 | sk_assert(u->tag1 == TempReg && VGC_IS_SHADOW(u->val1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1516 | if (def[u->val1] > 4) break; |
| 1517 | /* We now know that the arg to the op is entirely defined. |
| 1518 | If the op changes the size of the arg, we must replace |
| 1519 | it with a SETV at the new size. If it doesn't change |
| 1520 | the size, we can delete it completely. */ |
| 1521 | switch (u->val3) { |
| 1522 | /* Maintain the same size ... */ |
| 1523 | case Tag_Left4: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1524 | sk_assert(def[u->val1] == 4); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1525 | NOP_tag1_op(u); |
| 1526 | break; |
| 1527 | case Tag_PCast11: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1528 | sk_assert(def[u->val1] == 1); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1529 | NOP_tag1_op(u); |
| 1530 | break; |
| 1531 | /* Change size ... */ |
| 1532 | case Tag_PCast40: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1533 | sk_assert(def[u->val1] == 4); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1534 | SETV_tag1_op(u,0); |
| 1535 | def[u->val1] = 0; |
| 1536 | break; |
| 1537 | case Tag_PCast14: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1538 | sk_assert(def[u->val1] == 1); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1539 | SETV_tag1_op(u,4); |
| 1540 | def[u->val1] = 4; |
| 1541 | break; |
| 1542 | case Tag_PCast12: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1543 | sk_assert(def[u->val1] == 1); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1544 | SETV_tag1_op(u,2); |
| 1545 | def[u->val1] = 2; |
| 1546 | break; |
| 1547 | case Tag_PCast10: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1548 | sk_assert(def[u->val1] == 1); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1549 | SETV_tag1_op(u,0); |
| 1550 | def[u->val1] = 0; |
| 1551 | break; |
| 1552 | case Tag_PCast02: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1553 | sk_assert(def[u->val1] == 0); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1554 | SETV_tag1_op(u,2); |
| 1555 | def[u->val1] = 2; |
| 1556 | break; |
| 1557 | default: |
| 1558 | goto unhandled; |
| 1559 | } |
| 1560 | if (dis) |
| 1561 | VG_(printf)( |
| 1562 | " at %2d: delete TAG1 %s due to defd arg\n", |
| 1563 | i, nameOfTagOp(u->val3)); |
| 1564 | break; |
| 1565 | |
| 1566 | default: |
| 1567 | unhandled: |
| 1568 | /* We don't know how to handle this uinstr. Be safe, and |
| 1569 | set to VGC_VALUE or VGC_UNDEF all temps written by it. */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1570 | k = VG_(get_reg_usage)(u, TempReg, &tempUse[0], &isWrites[0]); |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1571 | sk_assert(0 <= k && k <= VG_MAX_REGS_USED); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1572 | for (j = 0; j < k; j++) { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1573 | t = tempUse[j]; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1574 | sk_assert(t >= 0 && t < n_temps); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1575 | if (!isWrites[j]) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1576 | /* t is read; ignore it. */ |
| 1577 | if (0&& VGC_IS_SHADOW(t) && def[t] <= 4) |
| 1578 | VG_(printf)("ignoring def %d at %s %s\n", |
| 1579 | def[t], |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1580 | VG_(name_UOpcode)(True, u->opcode), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1581 | (u->opcode == TAG1 || u->opcode == TAG2) |
| 1582 | ? nameOfTagOp(u->val3) |
| 1583 | : (Char*)""); |
| 1584 | } else { |
| 1585 | /* t is written; better nullify it. */ |
| 1586 | def[t] = VGC_IS_SHADOW(t) ? VGC_UNDEF : VGC_VALUE; |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | |
| 1594 | /* Top level post-MemCheck-instrumentation cleanup function. */ |
| 1595 | static void vg_cleanup ( UCodeBlock* cb ) |
| 1596 | { |
| 1597 | vg_propagate_definedness ( cb ); |
| 1598 | vg_delete_redundant_SETVs ( cb ); |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | /* Caller will print out final instrumented code if necessary; we |
| 1603 | print out intermediate instrumented code here if necessary. */ |
| 1604 | UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr not_used ) |
| 1605 | { |
| 1606 | cb = memcheck_instrument ( cb ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1607 | if (MC_(clo_cleanup)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1608 | if (dis) { |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1609 | VG_(pp_UCodeBlock) ( cb, "Unimproved instrumented UCode:" ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1610 | VG_(printf)("Instrumentation improvements:\n"); |
| 1611 | } |
| 1612 | vg_cleanup(cb); |
| 1613 | if (dis) VG_(printf)("\n"); |
| 1614 | } |
| 1615 | return cb; |
| 1616 | } |
| 1617 | |
| 1618 | #undef dis |
| 1619 | |
| 1620 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 1621 | /*--- end mc_translate.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1622 | /*--------------------------------------------------------------------*/ |