sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (test_main.c) is ---*/ |
| 5 | /*--- Copyright (c) 2004 OpenWorks LLP. All rights reserved. ---*/ |
| 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 11 | #include <assert.h> |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 12 | #include <string.h> |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 13 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 14 | #include "libvex_basictypes.h" |
| 15 | #include "libvex.h" |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 16 | |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 17 | #include "test_main.h" |
| 18 | |
| 19 | |
sewardj | c97096c | 2004-06-30 09:28:04 +0000 | [diff] [blame] | 20 | /*---------------------------------------------------------------*/ |
| 21 | /*--- Test ---*/ |
| 22 | /*---------------------------------------------------------------*/ |
| 23 | |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 24 | |
sewardj | 2b51587 | 2004-07-05 20:50:45 +0000 | [diff] [blame] | 25 | __attribute__ ((noreturn)) |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 26 | static |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 27 | void failure_exit ( void ) |
| 28 | { |
| 29 | fprintf(stdout, "VEX did failure_exit. Bye.\n"); |
| 30 | exit(1); |
| 31 | } |
| 32 | |
sewardj | 41f43bc | 2004-07-08 14:23:22 +0000 | [diff] [blame] | 33 | static |
sewardj | 5827784 | 2005-02-07 03:11:17 +0000 | [diff] [blame] | 34 | void log_bytes ( HChar* bytes, Int nbytes ) |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 35 | { |
| 36 | fwrite ( bytes, 1, nbytes, stdout ); |
| 37 | } |
| 38 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 39 | #define N_LINEBUF 10000 |
sewardj | de8c987 | 2005-03-25 13:48:55 +0000 | [diff] [blame] | 40 | static HChar linebuf[N_LINEBUF]; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 41 | |
sewardj | f05537e | 2004-10-14 01:08:12 +0000 | [diff] [blame] | 42 | #define N_ORIGBUF 1000 |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 43 | #define N_TRANSBUF 5000 |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 44 | |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 45 | static UChar origbuf[N_ORIGBUF]; |
| 46 | static UChar transbuf[N_TRANSBUF]; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 47 | |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 48 | static Bool verbose = True; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 49 | |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 50 | /* Forwards */ |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 51 | #if 0 /* UNUSED */ |
sewardj | 9578a8b | 2004-11-04 19:44:48 +0000 | [diff] [blame] | 52 | static IRBB* ac_instrument ( IRBB*, VexGuestLayout*, IRType ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 53 | static IRBB* mc_instrument ( IRBB*, VexGuestLayout*, IRType ); |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 54 | #endif |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 55 | |
sewardj | 5bd4d16 | 2004-11-10 13:02:48 +0000 | [diff] [blame] | 56 | static Bool chase_into_not_ok ( Addr64 dst ) { return False; } |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 57 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 58 | int main ( int argc, char** argv ) |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 59 | { |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 60 | FILE* f; |
| 61 | Int i; |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 62 | UInt u, sum; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 63 | Addr32 orig_addr; |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 64 | Int bb_number, n_bbs_done = 0; |
sewardj | 72c7281 | 2005-01-19 11:49:45 +0000 | [diff] [blame] | 65 | Int orig_nbytes, trans_used; |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 66 | VexTranslateResult tres; |
sewardj | b5bf2e0 | 2004-10-25 13:06:17 +0000 | [diff] [blame] | 67 | VexControl vcon; |
sewardj | 72c7281 | 2005-01-19 11:49:45 +0000 | [diff] [blame] | 68 | VexGuestExtents vge; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 69 | |
| 70 | if (argc != 2) { |
| 71 | fprintf(stderr, "usage: vex file.org\n"); |
| 72 | exit(1); |
| 73 | } |
| 74 | f = fopen(argv[1], "r"); |
| 75 | if (!f) { |
| 76 | fprintf(stderr, "can't open `%s'\n", argv[1]); |
| 77 | exit(1); |
| 78 | } |
| 79 | |
sewardj | b5bf2e0 | 2004-10-25 13:06:17 +0000 | [diff] [blame] | 80 | /* Run with default params. However, we can't allow bb chasing |
| 81 | since that causes the front end to get segfaults when it tries |
sewardj | 5bd4d16 | 2004-11-10 13:02:48 +0000 | [diff] [blame] | 82 | to read code outside the initial BB we hand it. So when calling |
| 83 | LibVEX_Translate, send in a chase-into predicate that always |
| 84 | returns False. */ |
sewardj | b5bf2e0 | 2004-10-25 13:06:17 +0000 | [diff] [blame] | 85 | LibVEX_default_VexControl ( &vcon ); |
sewardj | b495240 | 2004-10-26 13:24:50 +0000 | [diff] [blame] | 86 | vcon.iropt_level = 2; |
sewardj | c279b3f | 2005-03-16 18:10:45 +0000 | [diff] [blame] | 87 | vcon.guest_max_insns = 50; |
sewardj | b5bf2e0 | 2004-10-25 13:06:17 +0000 | [diff] [blame] | 88 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 89 | LibVEX_Init ( &failure_exit, &log_bytes, |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 90 | 1, /* debug_paranoia */ |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 91 | TEST_VSUPPORT, /* valgrind support */ |
sewardj | b5bf2e0 | 2004-10-25 13:06:17 +0000 | [diff] [blame] | 92 | &vcon ); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 93 | |
sewardj | ea64e14 | 2004-07-22 16:47:21 +0000 | [diff] [blame] | 94 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 95 | while (!feof(f)) { |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 96 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 97 | fgets(linebuf, N_LINEBUF,f); |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 98 | if (linebuf[0] == 0) continue; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 99 | if (linebuf[0] != '.') continue; |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 100 | |
| 101 | if (n_bbs_done == TEST_N_BBS) break; |
| 102 | n_bbs_done++; |
| 103 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 104 | /* first line is: . bb-number bb-addr n-bytes */ |
| 105 | assert(3 == sscanf(&linebuf[1], " %d %x %d\n", |
| 106 | & bb_number, |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 107 | & orig_addr, & orig_nbytes )); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 108 | assert(orig_nbytes >= 1); |
| 109 | assert(!feof(f)); |
| 110 | fgets(linebuf, N_LINEBUF,f); |
| 111 | assert(linebuf[0] == '.'); |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 112 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 113 | /* second line is: . byte byte byte etc */ |
sewardj | 1f40a0a | 2004-07-21 12:28:07 +0000 | [diff] [blame] | 114 | if (verbose) |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 115 | printf("============ Basic Block %d, Done %d, " |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 116 | "Start %x, nbytes %2d ============", |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 117 | bb_number, n_bbs_done-1, orig_addr, orig_nbytes); |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 118 | |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 119 | assert(orig_nbytes >= 1 && orig_nbytes <= N_ORIGBUF); |
| 120 | for (i = 0; i < orig_nbytes; i++) { |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 121 | assert(1 == sscanf(&linebuf[2 + 3*i], "%x", &u)); |
| 122 | origbuf[i] = (UChar)u; |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 125 | for (i = 0; i < TEST_N_ITERS; i++) |
| 126 | tres |
| 127 | = LibVEX_Translate ( |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 128 | #if 0 /* ppc32 -> ppc32 */ |
| 129 | VexArchPPC32, VexSubArchPPC32_noAV, |
| 130 | VexArchPPC32, VexSubArchPPC32_noAV, |
| 131 | #endif |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 132 | #if 0 /* ppc32 -> x86 */ |
| 133 | VexArchPPC32, VexSubArchPPC32_noAV, |
sewardj | c3263ee | 2004-12-29 17:48:22 +0000 | [diff] [blame] | 134 | VexArchX86, VexSubArchX86_sse2, |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 135 | #endif |
sewardj | c279b3f | 2005-03-16 18:10:45 +0000 | [diff] [blame] | 136 | #if 0 /* amd64 -> amd64 */ |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 137 | VexArchAMD64, VexSubArch_NONE, |
| 138 | VexArchAMD64, VexSubArch_NONE, |
| 139 | #endif |
sewardj | c279b3f | 2005-03-16 18:10:45 +0000 | [diff] [blame] | 140 | #if 1 /* x86 -> x86 */ |
| 141 | VexArchX86, VexSubArchX86_sse1, |
| 142 | VexArchX86, VexSubArchX86_sse1, |
| 143 | #endif |
sewardj | 07a54cc | 2005-02-04 21:18:55 +0000 | [diff] [blame] | 144 | |
sewardj | 5bd4d16 | 2004-11-10 13:02:48 +0000 | [diff] [blame] | 145 | origbuf, (Addr64)orig_addr, chase_into_not_ok, |
sewardj | 72c7281 | 2005-01-19 11:49:45 +0000 | [diff] [blame] | 146 | &vge, |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 147 | transbuf, N_TRANSBUF, &trans_used, |
sewardj | 9e7448b | 2004-11-18 13:44:15 +0000 | [diff] [blame] | 148 | #if 1 /* no instrumentation */ |
| 149 | NULL, /* instrument1 */ |
| 150 | NULL, /* instrument2 */ |
| 151 | False, /* cleanup after instrument */ |
| 152 | #endif |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 153 | #if 0 /* addrcheck */ |
| 154 | ac_instrument, /* instrument1 */ |
| 155 | NULL, /* instrument2 */ |
| 156 | False, /* cleanup after instrument */ |
| 157 | #endif |
sewardj | 9e7448b | 2004-11-18 13:44:15 +0000 | [diff] [blame] | 158 | #if 0 /* memcheck */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 159 | mc_instrument, /* instrument1 */ |
| 160 | NULL, /* instrument2 */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 161 | True, /* cleanup after instrument */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 162 | #endif |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 163 | NULL, /* access checker */ |
| 164 | TEST_FLAGS |
| 165 | ); |
| 166 | |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 167 | if (tres != VexTransOK) |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 168 | printf("\ntres = %d\n", (Int)tres); |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 169 | assert(tres == VexTransOK); |
sewardj | 72c7281 | 2005-01-19 11:49:45 +0000 | [diff] [blame] | 170 | assert(vge.n_used == 1); |
| 171 | assert((UInt)(vge.len[0]) == orig_nbytes); |
sewardj | abe017e | 2004-10-29 23:42:36 +0000 | [diff] [blame] | 172 | |
sewardj | 4459baa | 2004-09-10 20:00:46 +0000 | [diff] [blame] | 173 | sum = 0; |
| 174 | for (i = 0; i < trans_used; i++) |
| 175 | sum += (UInt)transbuf[i]; |
sewardj | de8c987 | 2005-03-25 13:48:55 +0000 | [diff] [blame] | 176 | printf ( " %6.2f ... %u\n", |
| 177 | (double)trans_used / (double)vge.len[0], sum ); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | fclose(f); |
sewardj | f05537e | 2004-10-14 01:08:12 +0000 | [diff] [blame] | 181 | printf("\n"); |
sewardj | d887b86 | 2005-01-17 18:34:34 +0000 | [diff] [blame] | 182 | LibVEX_ShowAllocStats(); |
sewardj | ced9fe5 | 2004-07-07 11:55:36 +0000 | [diff] [blame] | 183 | |
sewardj | ce605f9 | 2004-07-05 14:39:15 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 186 | |
| 187 | ////////////////////////////////////////////////////////////////////// |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 188 | ////////////////////////////////////////////////////////////////////// |
| 189 | ////////////////////////////////////////////////////////////////////// |
| 190 | ////////////////////////////////////////////////////////////////////// |
| 191 | ////////////////////////////////////////////////////////////////////// |
| 192 | ////////////////////////////////////////////////////////////////////// |
| 193 | ////////////////////////////////////////////////////////////////////// |
| 194 | ////////////////////////////////////////////////////////////////////// |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 195 | |
| 196 | static |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 197 | __attribute((noreturn)) |
sewardj | de8c987 | 2005-03-25 13:48:55 +0000 | [diff] [blame] | 198 | void panic ( HChar* s ) |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 199 | { |
| 200 | printf("\npanic: %s\n", s); |
| 201 | failure_exit(); |
| 202 | } |
| 203 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 204 | #if 0 /* UNUSED */ |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 205 | static |
sewardj | 9578a8b | 2004-11-04 19:44:48 +0000 | [diff] [blame] | 206 | IRBB* ac_instrument (IRBB* bb_in, VexGuestLayout* layout, IRType hWordTy ) |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 207 | { |
| 208 | /* Use this rather than eg. -1 because it's a UInt. */ |
| 209 | #define INVALID_DATA_SIZE 999999 |
| 210 | |
| 211 | Int i; |
| 212 | Int sz; |
| 213 | IRCallee* helper; |
| 214 | IRStmt* st; |
| 215 | IRExpr* data; |
| 216 | IRExpr* addr; |
| 217 | Bool needSz; |
| 218 | |
| 219 | /* Set up BB */ |
| 220 | IRBB* bb = emptyIRBB(); |
| 221 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 222 | bb->next = dopyIRExpr(bb_in->next); |
| 223 | bb->jumpkind = bb_in->jumpkind; |
| 224 | |
| 225 | /* No loads to consider in ->next. */ |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 226 | assert(isIRAtom(bb_in->next)); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 227 | |
| 228 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 229 | st = bb_in->stmts[i]; |
| 230 | if (!st) continue; |
| 231 | |
| 232 | switch (st->tag) { |
| 233 | |
| 234 | case Ist_Tmp: |
| 235 | data = st->Ist.Tmp.data; |
| 236 | if (data->tag == Iex_LDle) { |
| 237 | addr = data->Iex.LDle.addr; |
| 238 | sz = sizeofIRType(data->Iex.LDle.ty); |
| 239 | needSz = False; |
| 240 | switch (sz) { |
| 241 | case 4: helper = mkIRCallee(1, "ac_helperc_LOAD4", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 242 | (void*)0x12345601); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 243 | case 2: helper = mkIRCallee(0, "ac_helperc_LOAD2", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 244 | (void*)0x12345602); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 245 | case 1: helper = mkIRCallee(1, "ac_helperc_LOAD1", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 246 | (void*)0x12345603); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 247 | default: helper = mkIRCallee(0, "ac_helperc_LOADN", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 248 | (void*)0x12345604); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 249 | needSz = True; break; |
| 250 | } |
| 251 | if (needSz) { |
| 252 | addStmtToIRBB( |
| 253 | bb, |
| 254 | IRStmt_Dirty( |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 255 | unsafeIRDirty_0_N( helper->regparms, |
| 256 | helper->name, helper->addr, |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 257 | mkIRExprVec_2(addr, mkIRExpr_HWord(sz))) |
| 258 | )); |
| 259 | } else { |
| 260 | addStmtToIRBB( |
| 261 | bb, |
| 262 | IRStmt_Dirty( |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 263 | unsafeIRDirty_0_N( helper->regparms, |
| 264 | helper->name, helper->addr, |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 265 | mkIRExprVec_1(addr) ) |
| 266 | )); |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | |
| 271 | case Ist_STle: |
| 272 | data = st->Ist.STle.data; |
| 273 | addr = st->Ist.STle.addr; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 274 | assert(isIRAtom(data)); |
| 275 | assert(isIRAtom(addr)); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 276 | sz = sizeofIRType(typeOfIRExpr(bb_in->tyenv, data)); |
| 277 | needSz = False; |
| 278 | switch (sz) { |
| 279 | case 4: helper = mkIRCallee(1, "ac_helperc_STORE4", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 280 | (void*)0x12345605); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 281 | case 2: helper = mkIRCallee(0, "ac_helperc_STORE2", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 282 | (void*)0x12345606); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 283 | case 1: helper = mkIRCallee(1, "ac_helperc_STORE1", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 284 | (void*)0x12345607); break; |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 285 | default: helper = mkIRCallee(0, "ac_helperc_STOREN", |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 286 | (void*)0x12345608); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 287 | needSz = True; break; |
| 288 | } |
| 289 | if (needSz) { |
| 290 | addStmtToIRBB( |
| 291 | bb, |
| 292 | IRStmt_Dirty( |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 293 | unsafeIRDirty_0_N( helper->regparms, |
| 294 | helper->name, helper->addr, |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 295 | mkIRExprVec_2(addr, mkIRExpr_HWord(sz))) |
| 296 | )); |
| 297 | } else { |
| 298 | addStmtToIRBB( |
| 299 | bb, |
| 300 | IRStmt_Dirty( |
sewardj | f543586 | 2004-10-31 20:04:05 +0000 | [diff] [blame] | 301 | unsafeIRDirty_0_N( helper->regparms, |
| 302 | helper->name, helper->addr, |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 303 | mkIRExprVec_1(addr) ) |
| 304 | )); |
| 305 | } |
| 306 | break; |
| 307 | |
| 308 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 309 | assert(isIRAtom(st->Ist.Put.data)); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case Ist_PutI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 313 | assert(isIRAtom(st->Ist.PutI.ix)); |
| 314 | assert(isIRAtom(st->Ist.PutI.data)); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 315 | break; |
| 316 | |
| 317 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 318 | assert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 9b1004e | 2004-10-30 22:25:40 +0000 | [diff] [blame] | 319 | break; |
| 320 | |
| 321 | case Ist_Dirty: |
| 322 | /* If the call doesn't interact with memory, we ain't |
| 323 | interested. */ |
| 324 | if (st->Ist.Dirty.details->mFx == Ifx_None) |
| 325 | break; |
| 326 | goto unhandled; |
| 327 | |
| 328 | default: |
| 329 | unhandled: |
| 330 | printf("\n"); |
| 331 | ppIRStmt(st); |
| 332 | printf("\n"); |
| 333 | panic("addrcheck: unhandled IRStmt"); |
| 334 | } |
| 335 | |
| 336 | addStmtToIRBB( bb, dopyIRStmt(st)); |
| 337 | } |
| 338 | |
| 339 | return bb; |
| 340 | } |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 341 | #endif /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 342 | |
| 343 | ////////////////////////////////////////////////////////////////////// |
| 344 | ////////////////////////////////////////////////////////////////////// |
| 345 | ////////////////////////////////////////////////////////////////////// |
| 346 | ////////////////////////////////////////////////////////////////////// |
| 347 | ////////////////////////////////////////////////////////////////////// |
| 348 | ////////////////////////////////////////////////////////////////////// |
| 349 | ////////////////////////////////////////////////////////////////////// |
| 350 | ////////////////////////////////////////////////////////////////////// |
| 351 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 352 | #define tl_assert(xxx) assert(xxx) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 353 | #define VG_(xxxx) xxxx |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 354 | #define tool_panic(zzz) panic(zzz) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 355 | #define MC_(zzzz) MC_##zzzz |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 356 | #define TL_(zzzz) SK_##zzzz |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 357 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 358 | |
sewardj | de8c987 | 2005-03-25 13:48:55 +0000 | [diff] [blame] | 359 | static void MC_helperc_complain_undef ( void ); |
| 360 | static void MC_helperc_LOADV8 ( void ); |
| 361 | static void MC_helperc_LOADV4 ( void ); |
| 362 | static void MC_helperc_LOADV2 ( void ); |
| 363 | static void MC_helperc_LOADV1 ( void ); |
| 364 | //static void MC_helperc_STOREV8( void ); |
| 365 | //static void MC_helperc_STOREV4( void ); |
| 366 | //static void MC_helperc_STOREV2( void ); |
| 367 | //static void MC_helperc_STOREV1( void ); |
| 368 | static void MC_helperc_value_check0_fail( void ); |
| 369 | static void MC_helperc_value_check1_fail( void ); |
| 370 | static void MC_helperc_value_check4_fail( void ); |
| 371 | |
| 372 | static void MC_helperc_complain_undef ( void ) { } |
| 373 | static void MC_helperc_LOADV8 ( void ) { } |
| 374 | static void MC_helperc_LOADV4 ( void ) { } |
| 375 | static void MC_helperc_LOADV2 ( void ) { } |
| 376 | static void MC_helperc_LOADV1 ( void ) { } |
| 377 | //static void MC_helperc_STOREV8( void ) { } |
| 378 | //static void MC_helperc_STOREV4( void ) { } |
| 379 | //static void MC_helperc_STOREV2( void ) { } |
| 380 | //static void MC_helperc_STOREV1( void ) { } |
| 381 | static void MC_helperc_value_check0_fail( void ) { } |
| 382 | static void MC_helperc_value_check1_fail( void ) { } |
| 383 | static void MC_helperc_value_check4_fail( void ) { } |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 384 | |
| 385 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 386 | /*--------------------------------------------------------------------*/ |
| 387 | /*--- Instrument IR to perform memory checking operations. ---*/ |
| 388 | /*--- mc_translate.c ---*/ |
| 389 | /*--------------------------------------------------------------------*/ |
| 390 | |
| 391 | /* |
| 392 | This file is part of MemCheck, a heavyweight Valgrind tool for |
| 393 | detecting memory errors. |
| 394 | |
| 395 | Copyright (C) 2000-2004 Julian Seward |
| 396 | jseward@acm.org |
| 397 | |
| 398 | This program is free software; you can redistribute it and/or |
| 399 | modify it under the terms of the GNU General Public License as |
| 400 | published by the Free Software Foundation; either version 2 of the |
| 401 | License, or (at your option) any later version. |
| 402 | |
| 403 | This program is distributed in the hope that it will be useful, but |
| 404 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 405 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 406 | General Public License for more details. |
| 407 | |
| 408 | You should have received a copy of the GNU General Public License |
| 409 | along with this program; if not, write to the Free Software |
| 410 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 411 | 02111-1307, USA. |
| 412 | |
| 413 | The GNU General Public License is contained in the file COPYING. |
| 414 | */ |
| 415 | |
| 416 | //#include "mc_include.h" |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 417 | |
| 418 | |
| 419 | /*------------------------------------------------------------*/ |
| 420 | /*--- Forward decls ---*/ |
| 421 | /*------------------------------------------------------------*/ |
| 422 | |
| 423 | struct _MCEnv; |
| 424 | |
| 425 | static IRType shadowType ( IRType ty ); |
| 426 | static IRExpr* expr2vbits ( struct _MCEnv* mce, IRExpr* e ); |
| 427 | |
| 428 | |
| 429 | /*------------------------------------------------------------*/ |
| 430 | /*--- Memcheck running state, and tmp management. ---*/ |
| 431 | /*------------------------------------------------------------*/ |
| 432 | |
| 433 | /* Carries around state during memcheck instrumentation. */ |
| 434 | typedef |
| 435 | struct _MCEnv { |
| 436 | /* MODIFIED: the bb being constructed. IRStmts are added. */ |
| 437 | IRBB* bb; |
| 438 | |
| 439 | /* MODIFIED: a table [0 .. #temps_in_original_bb-1] which maps |
| 440 | original temps to their current their current shadow temp. |
| 441 | Initially all entries are IRTemp_INVALID. Entries are added |
| 442 | lazily since many original temps are not used due to |
| 443 | optimisation prior to instrumentation. Note that floating |
| 444 | point original tmps are shadowed by integer tmps of the same |
| 445 | size, and Bit-typed original tmps are shadowed by the type |
| 446 | Ity_I8. See comment below. */ |
| 447 | IRTemp* tmpMap; |
| 448 | Int n_originalTmps; /* for range checking */ |
| 449 | |
| 450 | /* READONLY: the guest layout. This indicates which parts of |
| 451 | the guest state should be regarded as 'always defined'. */ |
| 452 | VexGuestLayout* layout; |
| 453 | /* READONLY: the host word type. Needed for constructing |
| 454 | arguments of type 'HWord' to be passed to helper functions. |
| 455 | Ity_I32 or Ity_I64 only. */ |
| 456 | IRType hWordTy; |
| 457 | } |
| 458 | MCEnv; |
| 459 | |
| 460 | /* SHADOW TMP MANAGEMENT. Shadow tmps are allocated lazily (on |
| 461 | demand), as they are encountered. This is for two reasons. |
| 462 | |
| 463 | (1) (less important reason): Many original tmps are unused due to |
| 464 | initial IR optimisation, and we do not want to spaces in tables |
| 465 | tracking them. |
| 466 | |
| 467 | Shadow IRTemps are therefore allocated on demand. mce.tmpMap is a |
| 468 | table indexed [0 .. n_types-1], which gives the current shadow for |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 469 | each original tmp, or INVALID_IRTEMP if none is so far assigned. |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 470 | It is necessary to support making multiple assignments to a shadow |
| 471 | -- specifically, after testing a shadow for definedness, it needs |
| 472 | to be made defined. But IR's SSA property disallows this. |
| 473 | |
| 474 | (2) (more important reason): Therefore, when a shadow needs to get |
| 475 | a new value, a new temporary is created, the value is assigned to |
| 476 | that, and the tmpMap is updated to reflect the new binding. |
| 477 | |
| 478 | A corollary is that if the tmpMap maps a given tmp to |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 479 | INVALID_IRTEMP and we are hoping to read that shadow tmp, it means |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 480 | there's a read-before-write error in the original tmps. The IR |
| 481 | sanity checker should catch all such anomalies, however. |
| 482 | */ |
| 483 | |
| 484 | /* Find the tmp currently shadowing the given original tmp. If none |
| 485 | so far exists, allocate one. */ |
| 486 | static IRTemp findShadowTmp ( MCEnv* mce, IRTemp orig ) |
| 487 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 488 | tl_assert(orig < mce->n_originalTmps); |
sewardj | 92d168d | 2004-11-15 14:22:12 +0000 | [diff] [blame] | 489 | if (mce->tmpMap[orig] == IRTemp_INVALID) { |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 490 | mce->tmpMap[orig] |
| 491 | = newIRTemp(mce->bb->tyenv, |
| 492 | shadowType(mce->bb->tyenv->types[orig])); |
| 493 | } |
| 494 | return mce->tmpMap[orig]; |
| 495 | } |
| 496 | |
| 497 | /* Allocate a new shadow for the given original tmp. This means any |
| 498 | previous shadow is abandoned. This is needed because it is |
| 499 | necessary to give a new value to a shadow once it has been tested |
| 500 | for undefinedness, but unfortunately IR's SSA property disallows |
| 501 | this. Instead we must abandon the old shadow, allocate a new one |
| 502 | and use that instead. */ |
| 503 | static void newShadowTmp ( MCEnv* mce, IRTemp orig ) |
| 504 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 505 | tl_assert(orig < mce->n_originalTmps); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 506 | mce->tmpMap[orig] |
| 507 | = newIRTemp(mce->bb->tyenv, |
| 508 | shadowType(mce->bb->tyenv->types[orig])); |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /*------------------------------------------------------------*/ |
| 513 | /*--- IRAtoms -- a subset of IRExprs ---*/ |
| 514 | /*------------------------------------------------------------*/ |
| 515 | |
| 516 | /* An atom is either an IRExpr_Const or an IRExpr_Tmp, as defined by |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 517 | isIRAtom() in libvex_ir.h. Because this instrumenter expects flat |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 518 | input, most of this code deals in atoms. Usefully, a value atom |
| 519 | always has a V-value which is also an atom: constants are shadowed |
| 520 | by constants, and temps are shadowed by the corresponding shadow |
| 521 | temporary. */ |
| 522 | |
| 523 | typedef IRExpr IRAtom; |
| 524 | |
| 525 | /* (used for sanity checks only): is this an atom which looks |
| 526 | like it's from original code? */ |
| 527 | static Bool isOriginalAtom ( MCEnv* mce, IRAtom* a1 ) |
| 528 | { |
| 529 | if (a1->tag == Iex_Const) |
| 530 | return True; |
| 531 | if (a1->tag == Iex_Tmp && a1->Iex.Tmp.tmp < mce->n_originalTmps) |
| 532 | return True; |
| 533 | return False; |
| 534 | } |
| 535 | |
| 536 | /* (used for sanity checks only): is this an atom which looks |
| 537 | like it's from shadow code? */ |
| 538 | static Bool isShadowAtom ( MCEnv* mce, IRAtom* a1 ) |
| 539 | { |
| 540 | if (a1->tag == Iex_Const) |
| 541 | return True; |
| 542 | if (a1->tag == Iex_Tmp && a1->Iex.Tmp.tmp >= mce->n_originalTmps) |
| 543 | return True; |
| 544 | return False; |
| 545 | } |
| 546 | |
| 547 | /* (used for sanity checks only): check that both args are atoms and |
| 548 | are identically-kinded. */ |
| 549 | static Bool sameKindedAtoms ( IRAtom* a1, IRAtom* a2 ) |
| 550 | { |
| 551 | if (a1->tag == Iex_Tmp && a1->tag == Iex_Tmp) |
| 552 | return True; |
| 553 | if (a1->tag == Iex_Const && a1->tag == Iex_Const) |
| 554 | return True; |
| 555 | return False; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | /*------------------------------------------------------------*/ |
| 560 | /*--- Type management ---*/ |
| 561 | /*------------------------------------------------------------*/ |
| 562 | |
| 563 | /* Shadow state is always accessed using integer types. This returns |
| 564 | an integer type with the same size (as per sizeofIRType) as the |
| 565 | given type. The only valid shadow types are Bit, I8, I16, I32, |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 566 | I64, V128. */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 567 | |
| 568 | static IRType shadowType ( IRType ty ) |
| 569 | { |
| 570 | switch (ty) { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 571 | case Ity_I1: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 572 | case Ity_I8: |
| 573 | case Ity_I16: |
| 574 | case Ity_I32: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 575 | case Ity_I64: return ty; |
| 576 | case Ity_F32: return Ity_I32; |
| 577 | case Ity_F64: return Ity_I64; |
| 578 | case Ity_V128: return Ity_V128; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 579 | default: ppIRType(ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 580 | VG_(tool_panic)("memcheck:shadowType"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | /* Produce a 'defined' value of the given shadow type. Should only be |
| 585 | supplied shadow types (Bit/I8/I16/I32/UI64). */ |
| 586 | static IRExpr* definedOfType ( IRType ty ) { |
| 587 | switch (ty) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 588 | case Ity_I1: return IRExpr_Const(IRConst_U1(False)); |
| 589 | case Ity_I8: return IRExpr_Const(IRConst_U8(0)); |
| 590 | case Ity_I16: return IRExpr_Const(IRConst_U16(0)); |
| 591 | case Ity_I32: return IRExpr_Const(IRConst_U32(0)); |
| 592 | case Ity_I64: return IRExpr_Const(IRConst_U64(0)); |
| 593 | case Ity_V128: return IRExpr_Const(IRConst_V128(0x0000)); |
| 594 | default: VG_(tool_panic)("memcheck:definedOfType"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | /*------------------------------------------------------------*/ |
| 600 | /*--- Constructing IR fragments ---*/ |
| 601 | /*------------------------------------------------------------*/ |
| 602 | |
| 603 | /* assign value to tmp */ |
| 604 | #define assign(_bb,_tmp,_expr) \ |
| 605 | addStmtToIRBB((_bb), IRStmt_Tmp((_tmp),(_expr))) |
| 606 | |
| 607 | /* add stmt to a bb */ |
| 608 | #define stmt(_bb,_stmt) \ |
| 609 | addStmtToIRBB((_bb), (_stmt)) |
| 610 | |
| 611 | /* build various kinds of expressions */ |
| 612 | #define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2)) |
| 613 | #define unop(_op, _arg) IRExpr_Unop((_op),(_arg)) |
| 614 | #define mkU8(_n) IRExpr_Const(IRConst_U8(_n)) |
| 615 | #define mkU16(_n) IRExpr_Const(IRConst_U16(_n)) |
| 616 | #define mkU32(_n) IRExpr_Const(IRConst_U32(_n)) |
| 617 | #define mkU64(_n) IRExpr_Const(IRConst_U64(_n)) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 618 | #define mkV128(_n) IRExpr_Const(IRConst_V128(_n)) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 619 | #define mkexpr(_tmp) IRExpr_Tmp((_tmp)) |
| 620 | |
| 621 | /* bind the given expression to a new temporary, and return the |
| 622 | temporary. This effectively converts an arbitrary expression into |
| 623 | an atom. */ |
| 624 | static IRAtom* assignNew ( MCEnv* mce, IRType ty, IRExpr* e ) { |
| 625 | IRTemp t = newIRTemp(mce->bb->tyenv, ty); |
| 626 | assign(mce->bb, t, e); |
| 627 | return mkexpr(t); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /*------------------------------------------------------------*/ |
| 632 | /*--- Constructing definedness primitive ops ---*/ |
| 633 | /*------------------------------------------------------------*/ |
| 634 | |
| 635 | /* --------- Defined-if-either-defined --------- */ |
| 636 | |
| 637 | static IRAtom* mkDifD8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 638 | tl_assert(isShadowAtom(mce,a1)); |
| 639 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 640 | return assignNew(mce, Ity_I8, binop(Iop_And8, a1, a2)); |
| 641 | } |
| 642 | |
| 643 | static IRAtom* mkDifD16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 644 | tl_assert(isShadowAtom(mce,a1)); |
| 645 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 646 | return assignNew(mce, Ity_I16, binop(Iop_And16, a1, a2)); |
| 647 | } |
| 648 | |
| 649 | static IRAtom* mkDifD32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 650 | tl_assert(isShadowAtom(mce,a1)); |
| 651 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 652 | return assignNew(mce, Ity_I32, binop(Iop_And32, a1, a2)); |
| 653 | } |
| 654 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 655 | static IRAtom* mkDifD64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 656 | tl_assert(isShadowAtom(mce,a1)); |
| 657 | tl_assert(isShadowAtom(mce,a2)); |
| 658 | return assignNew(mce, Ity_I64, binop(Iop_And64, a1, a2)); |
| 659 | } |
| 660 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 661 | static IRAtom* mkDifDV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 662 | tl_assert(isShadowAtom(mce,a1)); |
| 663 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 664 | return assignNew(mce, Ity_V128, binop(Iop_AndV128, a1, a2)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 665 | } |
| 666 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 667 | /* --------- Undefined-if-either-undefined --------- */ |
| 668 | |
| 669 | static IRAtom* mkUifU8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 670 | tl_assert(isShadowAtom(mce,a1)); |
| 671 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 672 | return assignNew(mce, Ity_I8, binop(Iop_Or8, a1, a2)); |
| 673 | } |
| 674 | |
| 675 | static IRAtom* mkUifU16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 676 | tl_assert(isShadowAtom(mce,a1)); |
| 677 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 678 | return assignNew(mce, Ity_I16, binop(Iop_Or16, a1, a2)); |
| 679 | } |
| 680 | |
| 681 | static IRAtom* mkUifU32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 682 | tl_assert(isShadowAtom(mce,a1)); |
| 683 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 684 | return assignNew(mce, Ity_I32, binop(Iop_Or32, a1, a2)); |
| 685 | } |
| 686 | |
| 687 | static IRAtom* mkUifU64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 688 | tl_assert(isShadowAtom(mce,a1)); |
| 689 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 690 | return assignNew(mce, Ity_I64, binop(Iop_Or64, a1, a2)); |
| 691 | } |
| 692 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 693 | static IRAtom* mkUifUV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 694 | tl_assert(isShadowAtom(mce,a1)); |
| 695 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 696 | return assignNew(mce, Ity_V128, binop(Iop_OrV128, a1, a2)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static IRAtom* mkUifU ( MCEnv* mce, IRType vty, IRAtom* a1, IRAtom* a2 ) { |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 700 | switch (vty) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 701 | case Ity_I8: return mkUifU8(mce, a1, a2); |
| 702 | case Ity_I16: return mkUifU16(mce, a1, a2); |
| 703 | case Ity_I32: return mkUifU32(mce, a1, a2); |
| 704 | case Ity_I64: return mkUifU64(mce, a1, a2); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 705 | case Ity_V128: return mkUifUV128(mce, a1, a2); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 706 | default: |
| 707 | VG_(printf)("\n"); ppIRType(vty); VG_(printf)("\n"); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 708 | VG_(tool_panic)("memcheck:mkUifU"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /* --------- The Left-family of operations. --------- */ |
| 713 | |
| 714 | static IRAtom* mkLeft8 ( MCEnv* mce, IRAtom* a1 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 715 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 716 | /* It's safe to duplicate a1 since it's only an atom */ |
| 717 | return assignNew(mce, Ity_I8, |
| 718 | binop(Iop_Or8, a1, |
| 719 | assignNew(mce, Ity_I8, |
| 720 | /* unop(Iop_Neg8, a1)))); */ |
| 721 | binop(Iop_Sub8, mkU8(0), a1) ))); |
| 722 | } |
| 723 | |
| 724 | static IRAtom* mkLeft16 ( MCEnv* mce, IRAtom* a1 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 725 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 726 | /* It's safe to duplicate a1 since it's only an atom */ |
| 727 | return assignNew(mce, Ity_I16, |
| 728 | binop(Iop_Or16, a1, |
| 729 | assignNew(mce, Ity_I16, |
| 730 | /* unop(Iop_Neg16, a1)))); */ |
| 731 | binop(Iop_Sub16, mkU16(0), a1) ))); |
| 732 | } |
| 733 | |
| 734 | static IRAtom* mkLeft32 ( MCEnv* mce, IRAtom* a1 ) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 735 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 736 | /* It's safe to duplicate a1 since it's only an atom */ |
| 737 | return assignNew(mce, Ity_I32, |
| 738 | binop(Iop_Or32, a1, |
| 739 | assignNew(mce, Ity_I32, |
| 740 | /* unop(Iop_Neg32, a1)))); */ |
| 741 | binop(Iop_Sub32, mkU32(0), a1) ))); |
| 742 | } |
| 743 | |
| 744 | /* --------- 'Improvement' functions for AND/OR. --------- */ |
| 745 | |
| 746 | /* ImproveAND(data, vbits) = data OR vbits. Defined (0) data 0s give |
| 747 | defined (0); all other -> undefined (1). |
| 748 | */ |
| 749 | static IRAtom* mkImproveAND8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 750 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 751 | tl_assert(isOriginalAtom(mce, data)); |
| 752 | tl_assert(isShadowAtom(mce, vbits)); |
| 753 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 754 | return assignNew(mce, Ity_I8, binop(Iop_Or8, data, vbits)); |
| 755 | } |
| 756 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 757 | static IRAtom* mkImproveAND16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 758 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 759 | tl_assert(isOriginalAtom(mce, data)); |
| 760 | tl_assert(isShadowAtom(mce, vbits)); |
| 761 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 762 | return assignNew(mce, Ity_I16, binop(Iop_Or16, data, vbits)); |
| 763 | } |
| 764 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 765 | static IRAtom* mkImproveAND32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 766 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 767 | tl_assert(isOriginalAtom(mce, data)); |
| 768 | tl_assert(isShadowAtom(mce, vbits)); |
| 769 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 770 | return assignNew(mce, Ity_I32, binop(Iop_Or32, data, vbits)); |
| 771 | } |
| 772 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 773 | static IRAtom* mkImproveAND64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 774 | { |
| 775 | tl_assert(isOriginalAtom(mce, data)); |
| 776 | tl_assert(isShadowAtom(mce, vbits)); |
| 777 | tl_assert(sameKindedAtoms(data, vbits)); |
| 778 | return assignNew(mce, Ity_I64, binop(Iop_Or64, data, vbits)); |
| 779 | } |
| 780 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 781 | static IRAtom* mkImproveANDV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 782 | { |
| 783 | tl_assert(isOriginalAtom(mce, data)); |
| 784 | tl_assert(isShadowAtom(mce, vbits)); |
| 785 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 786 | return assignNew(mce, Ity_V128, binop(Iop_OrV128, data, vbits)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 787 | } |
| 788 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 789 | /* ImproveOR(data, vbits) = ~data OR vbits. Defined (0) data 1s give |
| 790 | defined (0); all other -> undefined (1). |
| 791 | */ |
| 792 | static IRAtom* mkImproveOR8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 793 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 794 | tl_assert(isOriginalAtom(mce, data)); |
| 795 | tl_assert(isShadowAtom(mce, vbits)); |
| 796 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 797 | return assignNew( |
| 798 | mce, Ity_I8, |
| 799 | binop(Iop_Or8, |
| 800 | assignNew(mce, Ity_I8, unop(Iop_Not8, data)), |
| 801 | vbits) ); |
| 802 | } |
| 803 | |
| 804 | static IRAtom* mkImproveOR16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 805 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 806 | tl_assert(isOriginalAtom(mce, data)); |
| 807 | tl_assert(isShadowAtom(mce, vbits)); |
| 808 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 809 | return assignNew( |
| 810 | mce, Ity_I16, |
| 811 | binop(Iop_Or16, |
| 812 | assignNew(mce, Ity_I16, unop(Iop_Not16, data)), |
| 813 | vbits) ); |
| 814 | } |
| 815 | |
| 816 | static IRAtom* mkImproveOR32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 817 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 818 | tl_assert(isOriginalAtom(mce, data)); |
| 819 | tl_assert(isShadowAtom(mce, vbits)); |
| 820 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 821 | return assignNew( |
| 822 | mce, Ity_I32, |
| 823 | binop(Iop_Or32, |
| 824 | assignNew(mce, Ity_I32, unop(Iop_Not32, data)), |
| 825 | vbits) ); |
| 826 | } |
| 827 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 828 | static IRAtom* mkImproveOR64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 829 | { |
| 830 | tl_assert(isOriginalAtom(mce, data)); |
| 831 | tl_assert(isShadowAtom(mce, vbits)); |
| 832 | tl_assert(sameKindedAtoms(data, vbits)); |
| 833 | return assignNew( |
| 834 | mce, Ity_I64, |
| 835 | binop(Iop_Or64, |
| 836 | assignNew(mce, Ity_I64, unop(Iop_Not64, data)), |
| 837 | vbits) ); |
| 838 | } |
| 839 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 840 | static IRAtom* mkImproveORV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 841 | { |
| 842 | tl_assert(isOriginalAtom(mce, data)); |
| 843 | tl_assert(isShadowAtom(mce, vbits)); |
| 844 | tl_assert(sameKindedAtoms(data, vbits)); |
| 845 | return assignNew( |
| 846 | mce, Ity_V128, |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 847 | binop(Iop_OrV128, |
| 848 | assignNew(mce, Ity_V128, unop(Iop_NotV128, data)), |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 849 | vbits) ); |
| 850 | } |
| 851 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 852 | /* --------- Pessimising casts. --------- */ |
| 853 | |
| 854 | static IRAtom* mkPCastTo( MCEnv* mce, IRType dst_ty, IRAtom* vbits ) |
| 855 | { |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 856 | IRType ty; |
| 857 | IRAtom* tmp1; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 858 | /* Note, dst_ty is a shadow type, not an original type. */ |
| 859 | /* First of all, collapse vbits down to a single bit. */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 860 | tl_assert(isShadowAtom(mce,vbits)); |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 861 | ty = typeOfIRExpr(mce->bb->tyenv, vbits); |
| 862 | tmp1 = NULL; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 863 | switch (ty) { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 864 | case Ity_I1: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 865 | tmp1 = vbits; |
| 866 | break; |
| 867 | case Ity_I8: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 868 | tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE8, vbits, mkU8(0))); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 869 | break; |
| 870 | case Ity_I16: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 871 | tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE16, vbits, mkU16(0))); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 872 | break; |
| 873 | case Ity_I32: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 874 | tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE32, vbits, mkU32(0))); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 875 | break; |
| 876 | case Ity_I64: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 877 | tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE64, vbits, mkU64(0))); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 878 | break; |
| 879 | default: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 880 | VG_(tool_panic)("mkPCastTo(1)"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 881 | } |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 882 | tl_assert(tmp1); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 883 | /* Now widen up to the dst type. */ |
| 884 | switch (dst_ty) { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 885 | case Ity_I1: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 886 | return tmp1; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 887 | case Ity_I8: |
| 888 | return assignNew(mce, Ity_I8, unop(Iop_1Sto8, tmp1)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 889 | case Ity_I16: |
| 890 | return assignNew(mce, Ity_I16, unop(Iop_1Sto16, tmp1)); |
| 891 | case Ity_I32: |
| 892 | return assignNew(mce, Ity_I32, unop(Iop_1Sto32, tmp1)); |
| 893 | case Ity_I64: |
| 894 | return assignNew(mce, Ity_I64, unop(Iop_1Sto64, tmp1)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 895 | case Ity_V128: |
| 896 | tmp1 = assignNew(mce, Ity_I64, unop(Iop_1Sto64, tmp1)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 897 | tmp1 = assignNew(mce, Ity_V128, binop(Iop_64HLtoV128, tmp1, tmp1)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 898 | return tmp1; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 899 | default: |
| 900 | ppIRType(dst_ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 901 | VG_(tool_panic)("mkPCastTo(2)"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /*------------------------------------------------------------*/ |
| 907 | /*--- Emit a test and complaint if something is undefined. ---*/ |
| 908 | /*------------------------------------------------------------*/ |
| 909 | |
| 910 | /* Set the annotations on a dirty helper to indicate that the stack |
| 911 | pointer and instruction pointers might be read. This is the |
| 912 | behaviour of all 'emit-a-complaint' style functions we might |
| 913 | call. */ |
| 914 | |
| 915 | static void setHelperAnns ( MCEnv* mce, IRDirty* di ) { |
| 916 | di->nFxState = 2; |
| 917 | di->fxState[0].fx = Ifx_Read; |
| 918 | di->fxState[0].offset = mce->layout->offset_SP; |
| 919 | di->fxState[0].size = mce->layout->sizeof_SP; |
| 920 | di->fxState[1].fx = Ifx_Read; |
| 921 | di->fxState[1].offset = mce->layout->offset_IP; |
| 922 | di->fxState[1].size = mce->layout->sizeof_IP; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | /* Check the supplied **original** atom for undefinedness, and emit a |
| 927 | complaint if so. Once that happens, mark it as defined. This is |
| 928 | possible because the atom is either a tmp or literal. If it's a |
| 929 | tmp, it will be shadowed by a tmp, and so we can set the shadow to |
| 930 | be defined. In fact as mentioned above, we will have to allocate a |
| 931 | new tmp to carry the new 'defined' shadow value, and update the |
| 932 | original->tmp mapping accordingly; we cannot simply assign a new |
| 933 | value to an existing shadow tmp as this breaks SSAness -- resulting |
| 934 | in the post-instrumentation sanity checker spluttering in disapproval. |
| 935 | */ |
| 936 | static void complainIfUndefined ( MCEnv* mce, IRAtom* atom ) |
| 937 | { |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 938 | IRAtom* vatom; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 939 | IRType ty; |
| 940 | Int sz; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 941 | IRDirty* di; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 942 | IRAtom* cond; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 943 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 944 | /* Since the original expression is atomic, there's no duplicated |
| 945 | work generated by making multiple V-expressions for it. So we |
| 946 | don't really care about the possibility that someone else may |
| 947 | also create a V-interpretion for it. */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 948 | tl_assert(isOriginalAtom(mce, atom)); |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 949 | vatom = expr2vbits( mce, atom ); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 950 | tl_assert(isShadowAtom(mce, vatom)); |
| 951 | tl_assert(sameKindedAtoms(atom, vatom)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 952 | |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 953 | ty = typeOfIRExpr(mce->bb->tyenv, vatom); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 954 | |
| 955 | /* sz is only used for constructing the error message */ |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 956 | sz = ty==Ity_I1 ? 0 : sizeofIRType(ty); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 957 | |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 958 | cond = mkPCastTo( mce, Ity_I1, vatom ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 959 | /* cond will be 0 if all defined, and 1 if any not defined. */ |
| 960 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 961 | switch (sz) { |
| 962 | case 0: |
| 963 | di = unsafeIRDirty_0_N( 0/*regparms*/, |
| 964 | "MC_(helperc_value_check0_fail)", |
| 965 | &MC_(helperc_value_check0_fail), |
| 966 | mkIRExprVec_0() |
| 967 | ); |
| 968 | break; |
| 969 | case 1: |
| 970 | di = unsafeIRDirty_0_N( 0/*regparms*/, |
| 971 | "MC_(helperc_value_check1_fail)", |
| 972 | &MC_(helperc_value_check1_fail), |
| 973 | mkIRExprVec_0() |
| 974 | ); |
| 975 | break; |
| 976 | case 4: |
| 977 | di = unsafeIRDirty_0_N( 0/*regparms*/, |
| 978 | "MC_(helperc_value_check4_fail)", |
| 979 | &MC_(helperc_value_check4_fail), |
| 980 | mkIRExprVec_0() |
| 981 | ); |
| 982 | break; |
| 983 | default: |
| 984 | di = unsafeIRDirty_0_N( 1/*regparms*/, |
| 985 | "MC_(helperc_complain_undef)", |
| 986 | &MC_(helperc_complain_undef), |
| 987 | mkIRExprVec_1( mkIRExpr_HWord( sz )) |
| 988 | ); |
| 989 | break; |
| 990 | } |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 991 | di->guard = cond; |
| 992 | setHelperAnns( mce, di ); |
| 993 | stmt( mce->bb, IRStmt_Dirty(di)); |
| 994 | |
| 995 | /* Set the shadow tmp to be defined. First, update the |
| 996 | orig->shadow tmp mapping to reflect the fact that this shadow is |
| 997 | getting a new value. */ |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 998 | tl_assert(isIRAtom(vatom)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 999 | /* sameKindedAtoms ... */ |
| 1000 | if (vatom->tag == Iex_Tmp) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1001 | tl_assert(atom->tag == Iex_Tmp); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1002 | newShadowTmp(mce, atom->Iex.Tmp.tmp); |
| 1003 | assign(mce->bb, findShadowTmp(mce, atom->Iex.Tmp.tmp), |
| 1004 | definedOfType(ty)); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /*------------------------------------------------------------*/ |
| 1010 | /*--- Shadowing PUTs/GETs, and indexed variants thereof ---*/ |
| 1011 | /*------------------------------------------------------------*/ |
| 1012 | |
| 1013 | /* Examine the always-defined sections declared in layout to see if |
| 1014 | the (offset,size) section is within one. Note, is is an error to |
| 1015 | partially fall into such a region: (offset,size) should either be |
| 1016 | completely in such a region or completely not-in such a region. |
| 1017 | */ |
| 1018 | static Bool isAlwaysDefd ( MCEnv* mce, Int offset, Int size ) |
| 1019 | { |
| 1020 | Int minoffD, maxoffD, i; |
| 1021 | Int minoff = offset; |
| 1022 | Int maxoff = minoff + size - 1; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1023 | tl_assert((minoff & ~0xFFFF) == 0); |
| 1024 | tl_assert((maxoff & ~0xFFFF) == 0); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1025 | |
| 1026 | for (i = 0; i < mce->layout->n_alwaysDefd; i++) { |
| 1027 | minoffD = mce->layout->alwaysDefd[i].offset; |
| 1028 | maxoffD = minoffD + mce->layout->alwaysDefd[i].size - 1; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1029 | tl_assert((minoffD & ~0xFFFF) == 0); |
| 1030 | tl_assert((maxoffD & ~0xFFFF) == 0); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (maxoff < minoffD || maxoffD < minoff) |
| 1033 | continue; /* no overlap */ |
| 1034 | if (minoff >= minoffD && maxoff <= maxoffD) |
| 1035 | return True; /* completely contained in an always-defd section */ |
| 1036 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1037 | VG_(tool_panic)("memcheck:isAlwaysDefd:partial overlap"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1038 | } |
| 1039 | return False; /* could not find any containing section */ |
| 1040 | } |
| 1041 | |
| 1042 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 1043 | #if 0 /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1044 | /* Generate into bb suitable actions to shadow this Put. If the state |
| 1045 | slice is marked 'always defined', do nothing. Otherwise, write the |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1046 | supplied V bits to the shadow state. We can pass in either an |
| 1047 | original atom or a V-atom, but not both. In the former case the |
| 1048 | relevant V-bits are then generated from the original. |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1049 | */ |
| 1050 | static |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1051 | void do_shadow_PUT ( MCEnv* mce, Int offset, |
| 1052 | IRAtom* atom, IRAtom* vatom ) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1053 | { |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1054 | IRType ty; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1055 | if (atom) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1056 | tl_assert(!vatom); |
| 1057 | tl_assert(isOriginalAtom(mce, atom)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1058 | vatom = expr2vbits( mce, atom ); |
| 1059 | } else { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1060 | tl_assert(vatom); |
| 1061 | tl_assert(isShadowAtom(mce, vatom)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1064 | ty = typeOfIRExpr(mce->bb->tyenv, vatom); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1065 | tl_assert(ty != Ity_I1); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1066 | if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) { |
| 1067 | /* later: no ... */ |
| 1068 | /* emit code to emit a complaint if any of the vbits are 1. */ |
| 1069 | /* complainIfUndefined(mce, atom); */ |
| 1070 | } else { |
| 1071 | /* Do a plain shadow Put. */ |
| 1072 | stmt( mce->bb, IRStmt_Put( offset + mce->layout->total_sizeB, vatom ) ); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | /* Return an expression which contains the V bits corresponding to the |
| 1078 | given GETI (passed in in pieces). |
| 1079 | */ |
| 1080 | static |
| 1081 | void do_shadow_PUTI ( MCEnv* mce, |
| 1082 | IRArray* descr, IRAtom* ix, Int bias, IRAtom* atom ) |
| 1083 | { |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1084 | IRAtom* vatom; |
| 1085 | IRType ty, tyS; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1086 | Int arrSize;; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1087 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1088 | tl_assert(isOriginalAtom(mce,atom)); |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1089 | vatom = expr2vbits( mce, atom ); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1090 | tl_assert(sameKindedAtoms(atom, vatom)); |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1091 | ty = descr->elemTy; |
| 1092 | tyS = shadowType(ty); |
| 1093 | arrSize = descr->nElems * sizeofIRType(ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1094 | tl_assert(ty != Ity_I1); |
| 1095 | tl_assert(isOriginalAtom(mce,ix)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1096 | complainIfUndefined(mce,ix); |
| 1097 | if (isAlwaysDefd(mce, descr->base, arrSize)) { |
| 1098 | /* later: no ... */ |
| 1099 | /* emit code to emit a complaint if any of the vbits are 1. */ |
| 1100 | /* complainIfUndefined(mce, atom); */ |
| 1101 | } else { |
| 1102 | /* Do a cloned version of the Put that refers to the shadow |
| 1103 | area. */ |
| 1104 | IRArray* new_descr |
| 1105 | = mkIRArray( descr->base + mce->layout->total_sizeB, |
| 1106 | tyS, descr->nElems); |
| 1107 | stmt( mce->bb, IRStmt_PutI( new_descr, ix, bias, vatom )); |
| 1108 | } |
| 1109 | } |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 1110 | #endif /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1111 | |
| 1112 | |
| 1113 | /* Return an expression which contains the V bits corresponding to the |
| 1114 | given GET (passed in in pieces). |
| 1115 | */ |
| 1116 | static |
| 1117 | IRExpr* shadow_GET ( MCEnv* mce, Int offset, IRType ty ) |
| 1118 | { |
| 1119 | IRType tyS = shadowType(ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1120 | tl_assert(ty != Ity_I1); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1121 | if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) { |
| 1122 | /* Always defined, return all zeroes of the relevant type */ |
| 1123 | return definedOfType(tyS); |
| 1124 | } else { |
| 1125 | /* return a cloned version of the Get that refers to the shadow |
| 1126 | area. */ |
| 1127 | return IRExpr_Get( offset + mce->layout->total_sizeB, tyS ); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | /* Return an expression which contains the V bits corresponding to the |
| 1133 | given GETI (passed in in pieces). |
| 1134 | */ |
| 1135 | static |
| 1136 | IRExpr* shadow_GETI ( MCEnv* mce, IRArray* descr, IRAtom* ix, Int bias ) |
| 1137 | { |
| 1138 | IRType ty = descr->elemTy; |
| 1139 | IRType tyS = shadowType(ty); |
| 1140 | Int arrSize = descr->nElems * sizeofIRType(ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1141 | tl_assert(ty != Ity_I1); |
| 1142 | tl_assert(isOriginalAtom(mce,ix)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1143 | complainIfUndefined(mce,ix); |
| 1144 | if (isAlwaysDefd(mce, descr->base, arrSize)) { |
| 1145 | /* Always defined, return all zeroes of the relevant type */ |
| 1146 | return definedOfType(tyS); |
| 1147 | } else { |
| 1148 | /* return a cloned version of the Get that refers to the shadow |
| 1149 | area. */ |
| 1150 | IRArray* new_descr |
| 1151 | = mkIRArray( descr->base + mce->layout->total_sizeB, |
| 1152 | tyS, descr->nElems); |
| 1153 | return IRExpr_GetI( new_descr, ix, bias ); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /*------------------------------------------------------------*/ |
| 1159 | /*--- Generating approximations for unknown operations, ---*/ |
| 1160 | /*--- using lazy-propagate semantics ---*/ |
| 1161 | /*------------------------------------------------------------*/ |
| 1162 | |
| 1163 | /* Lazy propagation of undefinedness from two values, resulting in the |
| 1164 | specified shadow type. |
| 1165 | */ |
| 1166 | static |
| 1167 | IRAtom* mkLazy2 ( MCEnv* mce, IRType finalVty, IRAtom* va1, IRAtom* va2 ) |
| 1168 | { |
| 1169 | /* force everything via 32-bit intermediaries. */ |
| 1170 | IRAtom* at; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1171 | tl_assert(isShadowAtom(mce,va1)); |
| 1172 | tl_assert(isShadowAtom(mce,va2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1173 | at = mkPCastTo(mce, Ity_I32, va1); |
| 1174 | at = mkUifU(mce, Ity_I32, at, mkPCastTo(mce, Ity_I32, va2)); |
| 1175 | at = mkPCastTo(mce, finalVty, at); |
| 1176 | return at; |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | /* Do the lazy propagation game from a null-terminated vector of |
| 1181 | atoms. This is presumably the arguments to a helper call, so the |
| 1182 | IRCallee info is also supplied in order that we can know which |
| 1183 | arguments should be ignored (via the .mcx_mask field). |
| 1184 | */ |
| 1185 | static |
| 1186 | IRAtom* mkLazyN ( MCEnv* mce, |
| 1187 | IRAtom** exprvec, IRType finalVtype, IRCallee* cee ) |
| 1188 | { |
| 1189 | Int i; |
| 1190 | IRAtom* here; |
| 1191 | IRAtom* curr = definedOfType(Ity_I32); |
| 1192 | for (i = 0; exprvec[i]; i++) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1193 | tl_assert(i < 32); |
| 1194 | tl_assert(isOriginalAtom(mce, exprvec[i])); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1195 | /* Only take notice of this arg if the callee's mc-exclusion |
| 1196 | mask does not say it is to be excluded. */ |
| 1197 | if (cee->mcx_mask & (1<<i)) { |
| 1198 | /* the arg is to be excluded from definedness checking. Do |
| 1199 | nothing. */ |
| 1200 | if (0) VG_(printf)("excluding %s(%d)\n", cee->name, i); |
| 1201 | } else { |
| 1202 | /* calculate the arg's definedness, and pessimistically merge |
| 1203 | it in. */ |
| 1204 | here = mkPCastTo( mce, Ity_I32, expr2vbits(mce, exprvec[i]) ); |
| 1205 | curr = mkUifU32(mce, here, curr); |
| 1206 | } |
| 1207 | } |
| 1208 | return mkPCastTo(mce, finalVtype, curr ); |
| 1209 | } |
| 1210 | |
| 1211 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 1212 | #if 0 /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1213 | /*------------------------------------------------------------*/ |
| 1214 | /*--- Generating expensive sequences for exact carry-chain ---*/ |
| 1215 | /*--- propagation in add/sub and related operations. ---*/ |
| 1216 | /*------------------------------------------------------------*/ |
| 1217 | |
| 1218 | static |
| 1219 | IRAtom* expensiveAdd32 ( MCEnv* mce, IRAtom* qaa, IRAtom* qbb, |
| 1220 | IRAtom* aa, IRAtom* bb ) |
| 1221 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1222 | IRAtom *a_min, *b_min, *a_max, *b_max; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1223 | IRType ty; |
| 1224 | IROp opAND, opOR, opXOR, opNOT, opADD; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1225 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1226 | tl_assert(isShadowAtom(mce,qaa)); |
| 1227 | tl_assert(isShadowAtom(mce,qbb)); |
| 1228 | tl_assert(isOriginalAtom(mce,aa)); |
| 1229 | tl_assert(isOriginalAtom(mce,bb)); |
| 1230 | tl_assert(sameKindedAtoms(qaa,aa)); |
| 1231 | tl_assert(sameKindedAtoms(qbb,bb)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1232 | |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 1233 | ty = Ity_I32; |
| 1234 | opAND = Iop_And32; |
| 1235 | opOR = Iop_Or32; |
| 1236 | opXOR = Iop_Xor32; |
| 1237 | opNOT = Iop_Not32; |
| 1238 | opADD = Iop_Add32; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1239 | |
| 1240 | // a_min = aa & ~qaa |
| 1241 | a_min = assignNew(mce,ty, |
| 1242 | binop(opAND, aa, |
| 1243 | assignNew(mce,ty, unop(opNOT, qaa)))); |
| 1244 | |
| 1245 | // b_min = bb & ~qbb |
| 1246 | b_min = assignNew(mce,ty, |
| 1247 | binop(opAND, bb, |
| 1248 | assignNew(mce,ty, unop(opNOT, qbb)))); |
| 1249 | |
| 1250 | // a_max = aa | qaa |
| 1251 | a_max = assignNew(mce,ty, binop(opOR, aa, qaa)); |
| 1252 | |
| 1253 | // b_max = bb | qbb |
| 1254 | b_max = assignNew(mce,ty, binop(opOR, bb, qbb)); |
| 1255 | |
| 1256 | // result = (qaa | qbb) | ((a_min + b_min) ^ (a_max + b_max)) |
| 1257 | return |
| 1258 | assignNew(mce,ty, |
| 1259 | binop( opOR, |
| 1260 | assignNew(mce,ty, binop(opOR, qaa, qbb)), |
| 1261 | assignNew(mce,ty, |
| 1262 | binop(opXOR, assignNew(mce,ty, binop(opADD, a_min, b_min)), |
| 1263 | assignNew(mce,ty, binop(opADD, a_max, b_max)) |
| 1264 | ) |
| 1265 | ) |
| 1266 | ) |
| 1267 | ); |
| 1268 | } |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 1269 | #endif /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1270 | |
| 1271 | |
| 1272 | /*------------------------------------------------------------*/ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1273 | /*--- Helpers for dealing with vector primops. ---*/ |
| 1274 | /*------------------------------------------------------------*/ |
| 1275 | |
| 1276 | /* Vector pessimisation -- pessimise within each lane individually. */ |
| 1277 | |
| 1278 | static IRAtom* mkPCast8x16 ( MCEnv* mce, IRAtom* at ) |
| 1279 | { |
| 1280 | return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ8x16, at)); |
| 1281 | } |
| 1282 | |
| 1283 | static IRAtom* mkPCast16x8 ( MCEnv* mce, IRAtom* at ) |
| 1284 | { |
| 1285 | return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ16x8, at)); |
| 1286 | } |
| 1287 | |
| 1288 | static IRAtom* mkPCast32x4 ( MCEnv* mce, IRAtom* at ) |
| 1289 | { |
| 1290 | return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ32x4, at)); |
| 1291 | } |
| 1292 | |
| 1293 | static IRAtom* mkPCast64x2 ( MCEnv* mce, IRAtom* at ) |
| 1294 | { |
| 1295 | return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ64x2, at)); |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | /* Here's a simple scheme capable of handling ops derived from SSE1 |
| 1300 | code and while only generating ops that can be efficiently |
| 1301 | implemented in SSE1. */ |
| 1302 | |
| 1303 | /* All-lanes versions are straightforward: |
| 1304 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1305 | binary32Fx4(x,y) ==> PCast32x4(UifUV128(x#,y#)) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1306 | |
| 1307 | unary32Fx4(x,y) ==> PCast32x4(x#) |
| 1308 | |
| 1309 | Lowest-lane-only versions are more complex: |
| 1310 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1311 | binary32F0x4(x,y) ==> SetV128lo32( |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1312 | x#, |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1313 | PCast32(V128to32(UifUV128(x#,y#))) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1314 | ) |
| 1315 | |
| 1316 | This is perhaps not so obvious. In particular, it's faster to |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1317 | do a V128-bit UifU and then take the bottom 32 bits than the more |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1318 | obvious scheme of taking the bottom 32 bits of each operand |
| 1319 | and doing a 32-bit UifU. Basically since UifU is fast and |
| 1320 | chopping lanes off vector values is slow. |
| 1321 | |
| 1322 | Finally: |
| 1323 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1324 | unary32F0x4(x) ==> SetV128lo32( |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1325 | x#, |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1326 | PCast32(V128to32(x#)) |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1327 | ) |
| 1328 | |
| 1329 | Where: |
| 1330 | |
| 1331 | PCast32(v#) = 1Sto32(CmpNE32(v#,0)) |
| 1332 | PCast32x4(v#) = CmpNEZ32x4(v#) |
| 1333 | */ |
| 1334 | |
| 1335 | static |
| 1336 | IRAtom* binary32Fx4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1337 | { |
| 1338 | IRAtom* at; |
| 1339 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1340 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1341 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1342 | at = assignNew(mce, Ity_V128, mkPCast32x4(mce, at)); |
| 1343 | return at; |
| 1344 | } |
| 1345 | |
| 1346 | static |
| 1347 | IRAtom* unary32Fx4 ( MCEnv* mce, IRAtom* vatomX ) |
| 1348 | { |
| 1349 | IRAtom* at; |
| 1350 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1351 | at = assignNew(mce, Ity_V128, mkPCast32x4(mce, vatomX)); |
| 1352 | return at; |
| 1353 | } |
| 1354 | |
| 1355 | static |
| 1356 | IRAtom* binary32F0x4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1357 | { |
| 1358 | IRAtom* at; |
| 1359 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1360 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1361 | at = mkUifUV128(mce, vatomX, vatomY); |
| 1362 | at = assignNew(mce, Ity_I32, unop(Iop_V128to32, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1363 | at = mkPCastTo(mce, Ity_I32, at); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1364 | at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1365 | return at; |
| 1366 | } |
| 1367 | |
| 1368 | static |
| 1369 | IRAtom* unary32F0x4 ( MCEnv* mce, IRAtom* vatomX ) |
| 1370 | { |
| 1371 | IRAtom* at; |
| 1372 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1373 | at = assignNew(mce, Ity_I32, unop(Iop_V128to32, vatomX)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1374 | at = mkPCastTo(mce, Ity_I32, at); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1375 | at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1376 | return at; |
| 1377 | } |
| 1378 | |
| 1379 | /* --- ... and ... 64Fx2 versions of the same ... --- */ |
| 1380 | |
| 1381 | static |
| 1382 | IRAtom* binary64Fx2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1383 | { |
| 1384 | IRAtom* at; |
| 1385 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1386 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1387 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1388 | at = assignNew(mce, Ity_V128, mkPCast64x2(mce, at)); |
| 1389 | return at; |
| 1390 | } |
| 1391 | |
| 1392 | static |
| 1393 | IRAtom* unary64Fx2 ( MCEnv* mce, IRAtom* vatomX ) |
| 1394 | { |
| 1395 | IRAtom* at; |
| 1396 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1397 | at = assignNew(mce, Ity_V128, mkPCast64x2(mce, vatomX)); |
| 1398 | return at; |
| 1399 | } |
| 1400 | |
| 1401 | static |
| 1402 | IRAtom* binary64F0x2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1403 | { |
| 1404 | IRAtom* at; |
| 1405 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1406 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1407 | at = mkUifUV128(mce, vatomX, vatomY); |
| 1408 | at = assignNew(mce, Ity_I64, unop(Iop_V128to64, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1409 | at = mkPCastTo(mce, Ity_I64, at); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1410 | at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1411 | return at; |
| 1412 | } |
| 1413 | |
| 1414 | static |
| 1415 | IRAtom* unary64F0x2 ( MCEnv* mce, IRAtom* vatomX ) |
| 1416 | { |
| 1417 | IRAtom* at; |
| 1418 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1419 | at = assignNew(mce, Ity_I64, unop(Iop_V128to64, vatomX)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1420 | at = mkPCastTo(mce, Ity_I64, at); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1421 | at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1422 | return at; |
| 1423 | } |
| 1424 | |
| 1425 | /* --- --- Vector saturated narrowing --- --- */ |
| 1426 | |
| 1427 | /* This is quite subtle. What to do is simple: |
| 1428 | |
| 1429 | Let the original narrowing op be QNarrowW{S,U}xN. Produce: |
| 1430 | |
| 1431 | the-narrowing-op( PCastWxN(vatom1), PCastWxN(vatom2)) |
| 1432 | |
| 1433 | Why this is right is not so simple. Consider a lane in the args, |
| 1434 | vatom1 or 2, doesn't matter. |
| 1435 | |
| 1436 | After the PCast, that lane is all 0s (defined) or all |
| 1437 | 1s(undefined). |
| 1438 | |
| 1439 | Both signed and unsigned saturating narrowing of all 0s produces |
| 1440 | all 0s, which is what we want. |
| 1441 | |
| 1442 | The all-1s case is more complex. Unsigned narrowing interprets an |
| 1443 | all-1s input as the largest unsigned integer, and so produces all |
| 1444 | 1s as a result since that is the largest unsigned value at the |
| 1445 | smaller width. |
| 1446 | |
| 1447 | Signed narrowing interprets all 1s as -1. Fortunately, -1 narrows |
| 1448 | to -1, so we still wind up with all 1s at the smaller width. |
| 1449 | |
| 1450 | So: In short, pessimise the args, then apply the original narrowing |
| 1451 | op. |
| 1452 | */ |
| 1453 | static |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1454 | IRAtom* vectorNarrowV128 ( MCEnv* mce, IROp narrow_op, |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1455 | IRAtom* vatom1, IRAtom* vatom2) |
| 1456 | { |
| 1457 | IRAtom *at1, *at2, *at3; |
| 1458 | IRAtom* (*pcast)( MCEnv*, IRAtom* ); |
| 1459 | switch (narrow_op) { |
| 1460 | case Iop_QNarrow32Sx4: pcast = mkPCast32x4; break; |
| 1461 | case Iop_QNarrow16Sx8: pcast = mkPCast16x8; break; |
| 1462 | case Iop_QNarrow16Ux8: pcast = mkPCast16x8; break; |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1463 | default: VG_(tool_panic)("vectorNarrowV128"); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1464 | } |
| 1465 | tl_assert(isShadowAtom(mce,vatom1)); |
| 1466 | tl_assert(isShadowAtom(mce,vatom2)); |
| 1467 | at1 = assignNew(mce, Ity_V128, pcast(mce, vatom1)); |
| 1468 | at2 = assignNew(mce, Ity_V128, pcast(mce, vatom2)); |
| 1469 | at3 = assignNew(mce, Ity_V128, binop(narrow_op, at1, at2)); |
| 1470 | return at3; |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | /* --- --- Vector integer arithmetic --- --- */ |
| 1475 | |
| 1476 | /* Simple ... UifU the args and per-lane pessimise the results. */ |
| 1477 | static |
| 1478 | IRAtom* binary8Ix16 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 1479 | { |
| 1480 | IRAtom* at; |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1481 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1482 | at = mkPCast8x16(mce, at); |
| 1483 | return at; |
| 1484 | } |
| 1485 | |
| 1486 | static |
| 1487 | IRAtom* binary16Ix8 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 1488 | { |
| 1489 | IRAtom* at; |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1490 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1491 | at = mkPCast16x8(mce, at); |
| 1492 | return at; |
| 1493 | } |
| 1494 | |
| 1495 | static |
| 1496 | IRAtom* binary32Ix4 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 1497 | { |
| 1498 | IRAtom* at; |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1499 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1500 | at = mkPCast32x4(mce, at); |
| 1501 | return at; |
| 1502 | } |
| 1503 | |
| 1504 | static |
| 1505 | IRAtom* binary64Ix2 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 1506 | { |
| 1507 | IRAtom* at; |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1508 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1509 | at = mkPCast64x2(mce, at); |
| 1510 | return at; |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | /*------------------------------------------------------------*/ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1515 | /*--- Generate shadow values from all kinds of IRExprs. ---*/ |
| 1516 | /*------------------------------------------------------------*/ |
| 1517 | |
| 1518 | static |
| 1519 | IRAtom* expr2vbits_Binop ( MCEnv* mce, |
| 1520 | IROp op, |
| 1521 | IRAtom* atom1, IRAtom* atom2 ) |
| 1522 | { |
| 1523 | IRType and_or_ty; |
| 1524 | IRAtom* (*uifu) (MCEnv*, IRAtom*, IRAtom*); |
| 1525 | IRAtom* (*difd) (MCEnv*, IRAtom*, IRAtom*); |
| 1526 | IRAtom* (*improve) (MCEnv*, IRAtom*, IRAtom*); |
| 1527 | |
| 1528 | IRAtom* vatom1 = expr2vbits( mce, atom1 ); |
| 1529 | IRAtom* vatom2 = expr2vbits( mce, atom2 ); |
| 1530 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1531 | tl_assert(isOriginalAtom(mce,atom1)); |
| 1532 | tl_assert(isOriginalAtom(mce,atom2)); |
| 1533 | tl_assert(isShadowAtom(mce,vatom1)); |
| 1534 | tl_assert(isShadowAtom(mce,vatom2)); |
| 1535 | tl_assert(sameKindedAtoms(atom1,vatom1)); |
| 1536 | tl_assert(sameKindedAtoms(atom2,vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1537 | switch (op) { |
| 1538 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1539 | /* V128-bit SIMD (SSE2-esque) */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1540 | |
| 1541 | case Iop_ShrN16x8: |
| 1542 | case Iop_ShrN32x4: |
| 1543 | case Iop_ShrN64x2: |
| 1544 | case Iop_SarN16x8: |
| 1545 | case Iop_SarN32x4: |
| 1546 | case Iop_ShlN16x8: |
| 1547 | case Iop_ShlN32x4: |
| 1548 | case Iop_ShlN64x2: |
| 1549 | /* Same scheme as with all other shifts. */ |
| 1550 | complainIfUndefined(mce, atom2); |
| 1551 | return assignNew(mce, Ity_V128, binop(op, vatom1, atom2)); |
| 1552 | |
| 1553 | case Iop_QSub8Ux16: |
| 1554 | case Iop_QSub8Sx16: |
| 1555 | case Iop_Sub8x16: |
| 1556 | case Iop_Min8Ux16: |
| 1557 | case Iop_Max8Ux16: |
| 1558 | case Iop_CmpGT8Sx16: |
| 1559 | case Iop_CmpEQ8x16: |
| 1560 | case Iop_Avg8Ux16: |
| 1561 | case Iop_QAdd8Ux16: |
| 1562 | case Iop_QAdd8Sx16: |
| 1563 | case Iop_Add8x16: |
| 1564 | return binary8Ix16(mce, vatom1, vatom2); |
| 1565 | |
| 1566 | case Iop_QSub16Ux8: |
| 1567 | case Iop_QSub16Sx8: |
| 1568 | case Iop_Sub16x8: |
| 1569 | case Iop_Mul16x8: |
| 1570 | case Iop_MulHi16Sx8: |
| 1571 | case Iop_MulHi16Ux8: |
| 1572 | case Iop_Min16Sx8: |
| 1573 | case Iop_Max16Sx8: |
| 1574 | case Iop_CmpGT16Sx8: |
| 1575 | case Iop_CmpEQ16x8: |
| 1576 | case Iop_Avg16Ux8: |
| 1577 | case Iop_QAdd16Ux8: |
| 1578 | case Iop_QAdd16Sx8: |
| 1579 | case Iop_Add16x8: |
| 1580 | return binary16Ix8(mce, vatom1, vatom2); |
| 1581 | |
| 1582 | case Iop_Sub32x4: |
| 1583 | case Iop_CmpGT32Sx4: |
| 1584 | case Iop_CmpEQ32x4: |
| 1585 | case Iop_Add32x4: |
| 1586 | return binary32Ix4(mce, vatom1, vatom2); |
| 1587 | |
| 1588 | case Iop_Sub64x2: |
| 1589 | case Iop_Add64x2: |
| 1590 | return binary64Ix2(mce, vatom1, vatom2); |
| 1591 | |
| 1592 | case Iop_QNarrow32Sx4: |
| 1593 | case Iop_QNarrow16Sx8: |
| 1594 | case Iop_QNarrow16Ux8: |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1595 | return vectorNarrowV128(mce, op, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1596 | |
| 1597 | case Iop_Sub64Fx2: |
| 1598 | case Iop_Mul64Fx2: |
| 1599 | case Iop_Min64Fx2: |
| 1600 | case Iop_Max64Fx2: |
| 1601 | case Iop_Div64Fx2: |
| 1602 | case Iop_CmpLT64Fx2: |
| 1603 | case Iop_CmpLE64Fx2: |
| 1604 | case Iop_CmpEQ64Fx2: |
| 1605 | case Iop_Add64Fx2: |
| 1606 | return binary64Fx2(mce, vatom1, vatom2); |
| 1607 | |
| 1608 | case Iop_Sub64F0x2: |
| 1609 | case Iop_Mul64F0x2: |
| 1610 | case Iop_Min64F0x2: |
| 1611 | case Iop_Max64F0x2: |
| 1612 | case Iop_Div64F0x2: |
| 1613 | case Iop_CmpLT64F0x2: |
| 1614 | case Iop_CmpLE64F0x2: |
| 1615 | case Iop_CmpEQ64F0x2: |
| 1616 | case Iop_Add64F0x2: |
| 1617 | return binary64F0x2(mce, vatom1, vatom2); |
| 1618 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1619 | /* V128-bit SIMD (SSE1-esque) */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1620 | |
| 1621 | case Iop_Sub32Fx4: |
| 1622 | case Iop_Mul32Fx4: |
| 1623 | case Iop_Min32Fx4: |
| 1624 | case Iop_Max32Fx4: |
| 1625 | case Iop_Div32Fx4: |
| 1626 | case Iop_CmpLT32Fx4: |
| 1627 | case Iop_CmpLE32Fx4: |
| 1628 | case Iop_CmpEQ32Fx4: |
| 1629 | case Iop_Add32Fx4: |
| 1630 | return binary32Fx4(mce, vatom1, vatom2); |
| 1631 | |
| 1632 | case Iop_Sub32F0x4: |
| 1633 | case Iop_Mul32F0x4: |
| 1634 | case Iop_Min32F0x4: |
| 1635 | case Iop_Max32F0x4: |
| 1636 | case Iop_Div32F0x4: |
| 1637 | case Iop_CmpLT32F0x4: |
| 1638 | case Iop_CmpLE32F0x4: |
| 1639 | case Iop_CmpEQ32F0x4: |
| 1640 | case Iop_Add32F0x4: |
| 1641 | return binary32F0x4(mce, vatom1, vatom2); |
| 1642 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1643 | /* V128-bit data-steering */ |
| 1644 | case Iop_SetV128lo32: |
| 1645 | case Iop_SetV128lo64: |
| 1646 | case Iop_64HLtoV128: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1647 | case Iop_InterleaveLO64x2: |
| 1648 | case Iop_InterleaveLO32x4: |
| 1649 | case Iop_InterleaveLO16x8: |
| 1650 | case Iop_InterleaveLO8x16: |
| 1651 | case Iop_InterleaveHI64x2: |
| 1652 | case Iop_InterleaveHI32x4: |
| 1653 | case Iop_InterleaveHI16x8: |
| 1654 | case Iop_InterleaveHI8x16: |
| 1655 | return assignNew(mce, Ity_V128, binop(op, vatom1, vatom2)); |
| 1656 | |
| 1657 | /* Scalar floating point */ |
| 1658 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1659 | case Iop_RoundF64: |
| 1660 | case Iop_F64toI64: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1661 | case Iop_I64toF64: |
| 1662 | /* First arg is I32 (rounding mode), second is F64 or I64 |
| 1663 | (data). */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1664 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 1665 | |
| 1666 | case Iop_PRemC3210F64: case Iop_PRem1C3210F64: |
| 1667 | /* Takes two F64 args. */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1668 | case Iop_F64toI32: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1669 | case Iop_F64toF32: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1670 | /* First arg is I32 (rounding mode), second is F64 (data). */ |
| 1671 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 1672 | |
| 1673 | case Iop_F64toI16: |
| 1674 | /* First arg is I32 (rounding mode), second is F64 (data). */ |
| 1675 | return mkLazy2(mce, Ity_I16, vatom1, vatom2); |
| 1676 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1677 | case Iop_ScaleF64: |
| 1678 | case Iop_Yl2xF64: |
| 1679 | case Iop_Yl2xp1F64: |
| 1680 | case Iop_PRemF64: |
| 1681 | case Iop_AtanF64: |
| 1682 | case Iop_AddF64: |
| 1683 | case Iop_DivF64: |
| 1684 | case Iop_SubF64: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1685 | case Iop_MulF64: |
| 1686 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 1687 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1688 | case Iop_CmpF64: |
| 1689 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 1690 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1691 | /* non-FP after here */ |
| 1692 | |
| 1693 | case Iop_DivModU64to32: |
| 1694 | case Iop_DivModS64to32: |
| 1695 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 1696 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1697 | case Iop_16HLto32: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1698 | return assignNew(mce, Ity_I32, binop(op, vatom1, vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1699 | case Iop_32HLto64: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1700 | return assignNew(mce, Ity_I64, binop(op, vatom1, vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1701 | |
| 1702 | case Iop_MullS32: |
| 1703 | case Iop_MullU32: { |
| 1704 | IRAtom* vLo32 = mkLeft32(mce, mkUifU32(mce, vatom1,vatom2)); |
| 1705 | IRAtom* vHi32 = mkPCastTo(mce, Ity_I32, vLo32); |
| 1706 | return assignNew(mce, Ity_I64, binop(Iop_32HLto64, vHi32, vLo32)); |
| 1707 | } |
| 1708 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1709 | case Iop_MullS16: |
| 1710 | case Iop_MullU16: { |
| 1711 | IRAtom* vLo16 = mkLeft16(mce, mkUifU16(mce, vatom1,vatom2)); |
| 1712 | IRAtom* vHi16 = mkPCastTo(mce, Ity_I16, vLo16); |
| 1713 | return assignNew(mce, Ity_I32, binop(Iop_16HLto32, vHi16, vLo16)); |
| 1714 | } |
| 1715 | |
| 1716 | case Iop_MullS8: |
| 1717 | case Iop_MullU8: { |
| 1718 | IRAtom* vLo8 = mkLeft8(mce, mkUifU8(mce, vatom1,vatom2)); |
| 1719 | IRAtom* vHi8 = mkPCastTo(mce, Ity_I8, vLo8); |
| 1720 | return assignNew(mce, Ity_I16, binop(Iop_8HLto16, vHi8, vLo8)); |
| 1721 | } |
| 1722 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1723 | case Iop_Add32: |
| 1724 | # if 0 |
| 1725 | return expensiveAdd32(mce, vatom1,vatom2, atom1,atom2); |
| 1726 | # endif |
| 1727 | case Iop_Sub32: |
| 1728 | case Iop_Mul32: |
| 1729 | return mkLeft32(mce, mkUifU32(mce, vatom1,vatom2)); |
| 1730 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1731 | case Iop_Mul16: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1732 | case Iop_Add16: |
| 1733 | case Iop_Sub16: |
| 1734 | return mkLeft16(mce, mkUifU16(mce, vatom1,vatom2)); |
| 1735 | |
| 1736 | case Iop_Sub8: |
| 1737 | case Iop_Add8: |
| 1738 | return mkLeft8(mce, mkUifU8(mce, vatom1,vatom2)); |
| 1739 | |
| 1740 | case Iop_CmpLE32S: case Iop_CmpLE32U: |
| 1741 | case Iop_CmpLT32U: case Iop_CmpLT32S: |
| 1742 | case Iop_CmpEQ32: case Iop_CmpNE32: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1743 | return mkPCastTo(mce, Ity_I1, mkUifU32(mce, vatom1,vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1744 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1745 | case Iop_CmpEQ16: case Iop_CmpNE16: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1746 | return mkPCastTo(mce, Ity_I1, mkUifU16(mce, vatom1,vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1747 | |
| 1748 | case Iop_CmpEQ8: case Iop_CmpNE8: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1749 | return mkPCastTo(mce, Ity_I1, mkUifU8(mce, vatom1,vatom2)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1750 | |
| 1751 | case Iop_Shl32: case Iop_Shr32: case Iop_Sar32: |
| 1752 | /* Complain if the shift amount is undefined. Then simply |
| 1753 | shift the first arg's V bits by the real shift amount. */ |
| 1754 | complainIfUndefined(mce, atom2); |
| 1755 | return assignNew(mce, Ity_I32, binop(op, vatom1, atom2)); |
| 1756 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1757 | case Iop_Shl16: case Iop_Shr16: case Iop_Sar16: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1758 | /* Same scheme as with 32-bit shifts. */ |
| 1759 | complainIfUndefined(mce, atom2); |
| 1760 | return assignNew(mce, Ity_I16, binop(op, vatom1, atom2)); |
| 1761 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1762 | case Iop_Shl8: case Iop_Shr8: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1763 | /* Same scheme as with 32-bit shifts. */ |
| 1764 | complainIfUndefined(mce, atom2); |
| 1765 | return assignNew(mce, Ity_I8, binop(op, vatom1, atom2)); |
| 1766 | |
| 1767 | case Iop_Shl64: case Iop_Shr64: |
| 1768 | /* Same scheme as with 32-bit shifts. */ |
| 1769 | complainIfUndefined(mce, atom2); |
| 1770 | return assignNew(mce, Ity_I64, binop(op, vatom1, atom2)); |
| 1771 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1772 | case Iop_AndV128: |
| 1773 | uifu = mkUifUV128; difd = mkDifDV128; |
| 1774 | and_or_ty = Ity_V128; improve = mkImproveANDV128; goto do_And_Or; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1775 | case Iop_And64: |
| 1776 | uifu = mkUifU64; difd = mkDifD64; |
| 1777 | and_or_ty = Ity_I64; improve = mkImproveAND64; goto do_And_Or; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1778 | case Iop_And32: |
| 1779 | uifu = mkUifU32; difd = mkDifD32; |
| 1780 | and_or_ty = Ity_I32; improve = mkImproveAND32; goto do_And_Or; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1781 | case Iop_And16: |
| 1782 | uifu = mkUifU16; difd = mkDifD16; |
| 1783 | and_or_ty = Ity_I16; improve = mkImproveAND16; goto do_And_Or; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1784 | case Iop_And8: |
| 1785 | uifu = mkUifU8; difd = mkDifD8; |
| 1786 | and_or_ty = Ity_I8; improve = mkImproveAND8; goto do_And_Or; |
| 1787 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1788 | case Iop_OrV128: |
| 1789 | uifu = mkUifUV128; difd = mkDifDV128; |
| 1790 | and_or_ty = Ity_V128; improve = mkImproveORV128; goto do_And_Or; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1791 | case Iop_Or64: |
| 1792 | uifu = mkUifU64; difd = mkDifD64; |
| 1793 | and_or_ty = Ity_I64; improve = mkImproveOR64; goto do_And_Or; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1794 | case Iop_Or32: |
| 1795 | uifu = mkUifU32; difd = mkDifD32; |
| 1796 | and_or_ty = Ity_I32; improve = mkImproveOR32; goto do_And_Or; |
| 1797 | case Iop_Or16: |
| 1798 | uifu = mkUifU16; difd = mkDifD16; |
| 1799 | and_or_ty = Ity_I16; improve = mkImproveOR16; goto do_And_Or; |
| 1800 | case Iop_Or8: |
| 1801 | uifu = mkUifU8; difd = mkDifD8; |
| 1802 | and_or_ty = Ity_I8; improve = mkImproveOR8; goto do_And_Or; |
| 1803 | |
| 1804 | do_And_Or: |
| 1805 | return |
| 1806 | assignNew( |
| 1807 | mce, |
| 1808 | and_or_ty, |
| 1809 | difd(mce, uifu(mce, vatom1, vatom2), |
| 1810 | difd(mce, improve(mce, atom1, vatom1), |
| 1811 | improve(mce, atom2, vatom2) ) ) ); |
| 1812 | |
| 1813 | case Iop_Xor8: |
| 1814 | return mkUifU8(mce, vatom1, vatom2); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1815 | case Iop_Xor16: |
| 1816 | return mkUifU16(mce, vatom1, vatom2); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1817 | case Iop_Xor32: |
| 1818 | return mkUifU32(mce, vatom1, vatom2); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1819 | case Iop_Xor64: |
| 1820 | return mkUifU64(mce, vatom1, vatom2); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1821 | case Iop_XorV128: |
| 1822 | return mkUifUV128(mce, vatom1, vatom2); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1823 | |
| 1824 | default: |
| 1825 | ppIROp(op); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1826 | VG_(tool_panic)("memcheck:expr2vbits_Binop"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | static |
| 1832 | IRExpr* expr2vbits_Unop ( MCEnv* mce, IROp op, IRAtom* atom ) |
| 1833 | { |
| 1834 | IRAtom* vatom = expr2vbits( mce, atom ); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1835 | tl_assert(isOriginalAtom(mce,atom)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1836 | switch (op) { |
| 1837 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1838 | case Iop_Sqrt64Fx2: |
| 1839 | return unary64Fx2(mce, vatom); |
| 1840 | |
| 1841 | case Iop_Sqrt64F0x2: |
| 1842 | return unary64F0x2(mce, vatom); |
| 1843 | |
| 1844 | case Iop_Sqrt32Fx4: |
| 1845 | case Iop_RSqrt32Fx4: |
| 1846 | case Iop_Recip32Fx4: |
| 1847 | return unary32Fx4(mce, vatom); |
| 1848 | |
| 1849 | case Iop_Sqrt32F0x4: |
| 1850 | case Iop_RSqrt32F0x4: |
| 1851 | case Iop_Recip32F0x4: |
| 1852 | return unary32F0x4(mce, vatom); |
| 1853 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1854 | case Iop_32UtoV128: |
| 1855 | case Iop_64UtoV128: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1856 | return assignNew(mce, Ity_V128, unop(op, vatom)); |
| 1857 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1858 | case Iop_F32toF64: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1859 | case Iop_I32toF64: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1860 | case Iop_NegF64: |
| 1861 | case Iop_SinF64: |
| 1862 | case Iop_CosF64: |
| 1863 | case Iop_TanF64: |
| 1864 | case Iop_SqrtF64: |
| 1865 | case Iop_AbsF64: |
| 1866 | case Iop_2xm1F64: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1867 | return mkPCastTo(mce, Ity_I64, vatom); |
| 1868 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1869 | case Iop_Clz32: |
| 1870 | case Iop_Ctz32: |
| 1871 | return mkPCastTo(mce, Ity_I32, vatom); |
| 1872 | |
| 1873 | case Iop_32Sto64: |
| 1874 | case Iop_32Uto64: |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1875 | case Iop_V128to64: |
| 1876 | case Iop_V128HIto64: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1877 | return assignNew(mce, Ity_I64, unop(op, vatom)); |
| 1878 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1879 | case Iop_64to32: |
| 1880 | case Iop_64HIto32: |
| 1881 | case Iop_1Uto32: |
| 1882 | case Iop_8Uto32: |
| 1883 | case Iop_16Uto32: |
| 1884 | case Iop_16Sto32: |
| 1885 | case Iop_8Sto32: |
| 1886 | return assignNew(mce, Ity_I32, unop(op, vatom)); |
| 1887 | |
| 1888 | case Iop_8Sto16: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1889 | case Iop_8Uto16: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1890 | case Iop_32to16: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1891 | case Iop_32HIto16: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1892 | return assignNew(mce, Ity_I16, unop(op, vatom)); |
| 1893 | |
| 1894 | case Iop_1Uto8: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1895 | case Iop_16to8: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1896 | case Iop_32to8: |
| 1897 | return assignNew(mce, Ity_I8, unop(op, vatom)); |
| 1898 | |
| 1899 | case Iop_32to1: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1900 | return assignNew(mce, Ity_I1, unop(Iop_32to1, vatom)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1901 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1902 | case Iop_ReinterpF64asI64: |
| 1903 | case Iop_ReinterpI64asF64: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1904 | case Iop_ReinterpI32asF32: |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1905 | case Iop_NotV128: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1906 | case Iop_Not64: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1907 | case Iop_Not32: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1908 | case Iop_Not16: |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1909 | case Iop_Not8: |
| 1910 | case Iop_Not1: |
| 1911 | return vatom; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1912 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1913 | default: |
| 1914 | ppIROp(op); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1915 | VG_(tool_panic)("memcheck:expr2vbits_Unop"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1920 | /* Worker function; do not call directly. */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1921 | static |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1922 | IRAtom* expr2vbits_LDle_WRK ( MCEnv* mce, IRType ty, IRAtom* addr, UInt bias ) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1923 | { |
| 1924 | void* helper; |
sewardj | de8c987 | 2005-03-25 13:48:55 +0000 | [diff] [blame] | 1925 | HChar* hname; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1926 | IRDirty* di; |
| 1927 | IRTemp datavbits; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1928 | IRAtom* addrAct; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1929 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1930 | tl_assert(isOriginalAtom(mce,addr)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1931 | |
| 1932 | /* First, emit a definedness test for the address. This also sets |
| 1933 | the address (shadow) to 'defined' following the test. */ |
| 1934 | complainIfUndefined( mce, addr ); |
| 1935 | |
| 1936 | /* Now cook up a call to the relevant helper function, to read the |
| 1937 | data V bits from shadow memory. */ |
| 1938 | ty = shadowType(ty); |
| 1939 | switch (ty) { |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1940 | case Ity_I64: helper = &MC_(helperc_LOADV8); |
| 1941 | hname = "MC_(helperc_LOADV8)"; |
| 1942 | break; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1943 | case Ity_I32: helper = &MC_(helperc_LOADV4); |
| 1944 | hname = "MC_(helperc_LOADV4)"; |
| 1945 | break; |
| 1946 | case Ity_I16: helper = &MC_(helperc_LOADV2); |
| 1947 | hname = "MC_(helperc_LOADV2)"; |
| 1948 | break; |
| 1949 | case Ity_I8: helper = &MC_(helperc_LOADV1); |
| 1950 | hname = "MC_(helperc_LOADV1)"; |
| 1951 | break; |
| 1952 | default: ppIRType(ty); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1953 | VG_(tool_panic)("memcheck:do_shadow_LDle"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1956 | /* Generate the actual address into addrAct. */ |
| 1957 | if (bias == 0) { |
| 1958 | addrAct = addr; |
| 1959 | } else { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1960 | IROp mkAdd; |
| 1961 | IRAtom* eBias; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1962 | IRType tyAddr = mce->hWordTy; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1963 | tl_assert( tyAddr == Ity_I32 || tyAddr == Ity_I64 ); |
| 1964 | mkAdd = tyAddr==Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 1965 | eBias = tyAddr==Ity_I32 ? mkU32(bias) : mkU64(bias); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1966 | addrAct = assignNew(mce, tyAddr, binop(mkAdd, addr, eBias) ); |
| 1967 | } |
| 1968 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1969 | /* We need to have a place to park the V bits we're just about to |
| 1970 | read. */ |
| 1971 | datavbits = newIRTemp(mce->bb->tyenv, ty); |
| 1972 | di = unsafeIRDirty_1_N( datavbits, |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 1973 | 1/*regparms*/, hname, helper, |
| 1974 | mkIRExprVec_1( addrAct )); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 1975 | setHelperAnns( mce, di ); |
| 1976 | stmt( mce->bb, IRStmt_Dirty(di) ); |
| 1977 | |
| 1978 | return mkexpr(datavbits); |
| 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | static |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1983 | IRAtom* expr2vbits_LDle ( MCEnv* mce, IRType ty, IRAtom* addr, UInt bias ) |
| 1984 | { |
| 1985 | IRAtom *v64hi, *v64lo; |
| 1986 | switch (shadowType(ty)) { |
| 1987 | case Ity_I8: |
| 1988 | case Ity_I16: |
| 1989 | case Ity_I32: |
| 1990 | case Ity_I64: |
| 1991 | return expr2vbits_LDle_WRK(mce, ty, addr, bias); |
| 1992 | case Ity_V128: |
| 1993 | v64lo = expr2vbits_LDle_WRK(mce, Ity_I64, addr, bias); |
| 1994 | v64hi = expr2vbits_LDle_WRK(mce, Ity_I64, addr, bias+8); |
| 1995 | return assignNew( mce, |
| 1996 | Ity_V128, |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 1997 | binop(Iop_64HLtoV128, v64hi, v64lo)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 1998 | default: |
| 1999 | VG_(tool_panic)("expr2vbits_LDle"); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | |
| 2004 | static |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2005 | IRAtom* expr2vbits_Mux0X ( MCEnv* mce, |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2006 | IRAtom* cond, IRAtom* expr0, IRAtom* exprX ) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2007 | { |
| 2008 | IRAtom *vbitsC, *vbits0, *vbitsX; |
| 2009 | IRType ty; |
| 2010 | /* Given Mux0X(cond,expr0,exprX), generate |
| 2011 | Mux0X(cond,expr0#,exprX#) `UifU` PCast(cond#) |
| 2012 | That is, steer the V bits like the originals, but trash the |
| 2013 | result if the steering value is undefined. This gives |
| 2014 | lazy propagation. */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2015 | tl_assert(isOriginalAtom(mce, cond)); |
| 2016 | tl_assert(isOriginalAtom(mce, expr0)); |
| 2017 | tl_assert(isOriginalAtom(mce, exprX)); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2018 | |
| 2019 | vbitsC = expr2vbits(mce, cond); |
| 2020 | vbits0 = expr2vbits(mce, expr0); |
| 2021 | vbitsX = expr2vbits(mce, exprX); |
| 2022 | ty = typeOfIRExpr(mce->bb->tyenv, vbits0); |
| 2023 | |
| 2024 | return |
| 2025 | mkUifU(mce, ty, assignNew(mce, ty, IRExpr_Mux0X(cond, vbits0, vbitsX)), |
| 2026 | mkPCastTo(mce, ty, vbitsC) ); |
| 2027 | } |
| 2028 | |
| 2029 | /* --------- This is the main expression-handling function. --------- */ |
| 2030 | |
| 2031 | static |
| 2032 | IRExpr* expr2vbits ( MCEnv* mce, IRExpr* e ) |
| 2033 | { |
| 2034 | switch (e->tag) { |
| 2035 | |
| 2036 | case Iex_Get: |
| 2037 | return shadow_GET( mce, e->Iex.Get.offset, e->Iex.Get.ty ); |
| 2038 | |
| 2039 | case Iex_GetI: |
| 2040 | return shadow_GETI( mce, e->Iex.GetI.descr, |
| 2041 | e->Iex.GetI.ix, e->Iex.GetI.bias ); |
| 2042 | |
| 2043 | case Iex_Tmp: |
| 2044 | return IRExpr_Tmp( findShadowTmp(mce, e->Iex.Tmp.tmp) ); |
| 2045 | |
| 2046 | case Iex_Const: |
| 2047 | return definedOfType(shadowType(typeOfIRExpr(mce->bb->tyenv, e))); |
| 2048 | |
| 2049 | case Iex_Binop: |
| 2050 | return expr2vbits_Binop( |
| 2051 | mce, |
| 2052 | e->Iex.Binop.op, |
| 2053 | e->Iex.Binop.arg1, e->Iex.Binop.arg2 |
| 2054 | ); |
| 2055 | |
| 2056 | case Iex_Unop: |
| 2057 | return expr2vbits_Unop( mce, e->Iex.Unop.op, e->Iex.Unop.arg ); |
| 2058 | |
| 2059 | case Iex_LDle: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2060 | return expr2vbits_LDle( mce, e->Iex.LDle.ty, |
| 2061 | e->Iex.LDle.addr, 0/*addr bias*/ ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2062 | |
| 2063 | case Iex_CCall: |
| 2064 | return mkLazyN( mce, e->Iex.CCall.args, |
| 2065 | e->Iex.CCall.retty, |
| 2066 | e->Iex.CCall.cee ); |
| 2067 | |
| 2068 | case Iex_Mux0X: |
| 2069 | return expr2vbits_Mux0X( mce, e->Iex.Mux0X.cond, e->Iex.Mux0X.expr0, |
| 2070 | e->Iex.Mux0X.exprX); |
| 2071 | |
| 2072 | default: |
| 2073 | VG_(printf)("\n"); |
| 2074 | ppIRExpr(e); |
| 2075 | VG_(printf)("\n"); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2076 | VG_(tool_panic)("memcheck: expr2vbits"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 2080 | #if 0 /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2081 | /*------------------------------------------------------------*/ |
| 2082 | /*--- Generate shadow stmts from all kinds of IRStmts. ---*/ |
| 2083 | /*------------------------------------------------------------*/ |
| 2084 | |
| 2085 | /* Widen a value to the host word size. */ |
| 2086 | |
| 2087 | static |
| 2088 | IRExpr* zwidenToHostWord ( MCEnv* mce, IRAtom* vatom ) |
| 2089 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2090 | IRType ty, tyH; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2091 | |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 2092 | /* vatom is vbits-value and as such can only have a shadow type. */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2093 | tl_assert(isShadowAtom(mce,vatom)); |
| 2094 | |
| 2095 | ty = typeOfIRExpr(mce->bb->tyenv, vatom); |
| 2096 | tyH = mce->hWordTy; |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 2097 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2098 | if (tyH == Ity_I32) { |
| 2099 | switch (ty) { |
| 2100 | case Ity_I32: return vatom; |
| 2101 | case Ity_I16: return assignNew(mce, tyH, unop(Iop_16Uto32, vatom)); |
| 2102 | case Ity_I8: return assignNew(mce, tyH, unop(Iop_8Uto32, vatom)); |
| 2103 | default: goto unhandled; |
| 2104 | } |
| 2105 | } else { |
| 2106 | goto unhandled; |
| 2107 | } |
| 2108 | unhandled: |
| 2109 | VG_(printf)("\nty = "); ppIRType(ty); VG_(printf)("\n"); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2110 | VG_(tool_panic)("zwidenToHostWord"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2114 | /* Generate a shadow store. addr is always the original address atom. |
| 2115 | You can pass in either originals or V-bits for the data atom, but |
| 2116 | obviously not both. */ |
| 2117 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2118 | static |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2119 | void do_shadow_STle ( MCEnv* mce, |
| 2120 | IRAtom* addr, UInt bias, |
| 2121 | IRAtom* data, IRAtom* vdata ) |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2122 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2123 | IROp mkAdd; |
| 2124 | IRType ty, tyAddr; |
| 2125 | IRDirty *di, *diLo64, *diHi64; |
| 2126 | IRAtom *addrAct, *addrLo64, *addrHi64; |
| 2127 | IRAtom *vdataLo64, *vdataHi64; |
| 2128 | IRAtom *eBias, *eBias0, *eBias8; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2129 | void* helper = NULL; |
| 2130 | Char* hname = NULL; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2131 | |
| 2132 | tyAddr = mce->hWordTy; |
| 2133 | mkAdd = tyAddr==Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 2134 | tl_assert( tyAddr == Ity_I32 || tyAddr == Ity_I64 ); |
| 2135 | |
| 2136 | di = diLo64 = diHi64 = NULL; |
| 2137 | eBias = eBias0 = eBias8 = NULL; |
| 2138 | addrAct = addrLo64 = addrHi64 = NULL; |
| 2139 | vdataLo64 = vdataHi64 = NULL; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2140 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2141 | if (data) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2142 | tl_assert(!vdata); |
| 2143 | tl_assert(isOriginalAtom(mce, data)); |
| 2144 | tl_assert(bias == 0); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2145 | vdata = expr2vbits( mce, data ); |
| 2146 | } else { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2147 | tl_assert(vdata); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2148 | } |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2149 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2150 | tl_assert(isOriginalAtom(mce,addr)); |
| 2151 | tl_assert(isShadowAtom(mce,vdata)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2152 | |
| 2153 | ty = typeOfIRExpr(mce->bb->tyenv, vdata); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* First, emit a definedness test for the address. This also sets |
| 2156 | the address (shadow) to 'defined' following the test. */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2157 | complainIfUndefined( mce, addr ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2158 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2159 | /* Now decide which helper function to call to write the data V |
| 2160 | bits into shadow memory. */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2161 | switch (ty) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2162 | case Ity_V128: /* we'll use the helper twice */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2163 | case Ity_I64: helper = &MC_(helperc_STOREV8); |
| 2164 | hname = "MC_(helperc_STOREV8)"; |
| 2165 | break; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2166 | case Ity_I32: helper = &MC_(helperc_STOREV4); |
| 2167 | hname = "MC_(helperc_STOREV4)"; |
| 2168 | break; |
| 2169 | case Ity_I16: helper = &MC_(helperc_STOREV2); |
| 2170 | hname = "MC_(helperc_STOREV2)"; |
| 2171 | break; |
| 2172 | case Ity_I8: helper = &MC_(helperc_STOREV1); |
| 2173 | hname = "MC_(helperc_STOREV1)"; |
| 2174 | break; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2175 | default: VG_(tool_panic)("memcheck:do_shadow_STle"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2178 | if (ty == Ity_V128) { |
| 2179 | |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 2180 | /* V128-bit case */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2181 | /* See comment in next clause re 64-bit regparms */ |
| 2182 | eBias0 = tyAddr==Ity_I32 ? mkU32(bias) : mkU64(bias); |
| 2183 | addrLo64 = assignNew(mce, tyAddr, binop(mkAdd, addr, eBias0) ); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 2184 | vdataLo64 = assignNew(mce, Ity_I64, unop(Iop_V128to64, vdata)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2185 | diLo64 = unsafeIRDirty_0_N( |
| 2186 | 1/*regparms*/, hname, helper, |
| 2187 | mkIRExprVec_2( addrLo64, vdataLo64 )); |
| 2188 | |
| 2189 | eBias8 = tyAddr==Ity_I32 ? mkU32(bias+8) : mkU64(bias+8); |
| 2190 | addrHi64 = assignNew(mce, tyAddr, binop(mkAdd, addr, eBias8) ); |
sewardj | f0c1c58 | 2005-02-07 23:47:38 +0000 | [diff] [blame] | 2191 | vdataHi64 = assignNew(mce, Ity_I64, unop(Iop_V128HIto64, vdata)); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2192 | diHi64 = unsafeIRDirty_0_N( |
| 2193 | 1/*regparms*/, hname, helper, |
| 2194 | mkIRExprVec_2( addrHi64, vdataHi64 )); |
| 2195 | |
| 2196 | setHelperAnns( mce, diLo64 ); |
| 2197 | setHelperAnns( mce, diHi64 ); |
| 2198 | stmt( mce->bb, IRStmt_Dirty(diLo64) ); |
| 2199 | stmt( mce->bb, IRStmt_Dirty(diHi64) ); |
| 2200 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2201 | } else { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* 8/16/32/64-bit cases */ |
| 2204 | /* Generate the actual address into addrAct. */ |
| 2205 | if (bias == 0) { |
| 2206 | addrAct = addr; |
| 2207 | } else { |
| 2208 | eBias = tyAddr==Ity_I32 ? mkU32(bias) : mkU64(bias); |
| 2209 | addrAct = assignNew(mce, tyAddr, binop(mkAdd, addr, eBias) ); |
| 2210 | } |
| 2211 | |
| 2212 | if (ty == Ity_I64) { |
| 2213 | /* We can't do this with regparm 2 on 32-bit platforms, since |
| 2214 | the back ends aren't clever enough to handle 64-bit |
| 2215 | regparm args. Therefore be different. */ |
| 2216 | di = unsafeIRDirty_0_N( |
| 2217 | 1/*regparms*/, hname, helper, |
| 2218 | mkIRExprVec_2( addrAct, vdata )); |
| 2219 | } else { |
| 2220 | di = unsafeIRDirty_0_N( |
| 2221 | 2/*regparms*/, hname, helper, |
| 2222 | mkIRExprVec_2( addrAct, |
| 2223 | zwidenToHostWord( mce, vdata ))); |
| 2224 | } |
| 2225 | setHelperAnns( mce, di ); |
| 2226 | stmt( mce->bb, IRStmt_Dirty(di) ); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2232 | /* Do lazy pessimistic propagation through a dirty helper call, by |
| 2233 | looking at the annotations on it. This is the most complex part of |
| 2234 | Memcheck. */ |
| 2235 | |
| 2236 | static IRType szToITy ( Int n ) |
| 2237 | { |
| 2238 | switch (n) { |
| 2239 | case 1: return Ity_I8; |
| 2240 | case 2: return Ity_I16; |
| 2241 | case 4: return Ity_I32; |
| 2242 | case 8: return Ity_I64; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2243 | default: VG_(tool_panic)("szToITy(memcheck)"); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | static |
| 2248 | void do_shadow_Dirty ( MCEnv* mce, IRDirty* d ) |
| 2249 | { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2250 | Int i, n, offset, toDo, gSz, gOff; |
| 2251 | IRAtom *src, *here, *curr; |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2252 | IRType tyAddr, tySrc, tyDst; |
| 2253 | IRTemp dst; |
| 2254 | |
| 2255 | /* First check the guard. */ |
| 2256 | complainIfUndefined(mce, d->guard); |
| 2257 | |
| 2258 | /* Now round up all inputs and PCast over them. */ |
sewardj | 8fc9374 | 2004-11-22 11:29:33 +0000 | [diff] [blame] | 2259 | curr = definedOfType(Ity_I32); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2260 | |
| 2261 | /* Inputs: unmasked args */ |
| 2262 | for (i = 0; d->args[i]; i++) { |
| 2263 | if (d->cee->mcx_mask & (1<<i)) { |
| 2264 | /* ignore this arg */ |
| 2265 | } else { |
| 2266 | here = mkPCastTo( mce, Ity_I32, expr2vbits(mce, d->args[i]) ); |
| 2267 | curr = mkUifU32(mce, here, curr); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | /* Inputs: guest state that we read. */ |
| 2272 | for (i = 0; i < d->nFxState; i++) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2273 | tl_assert(d->fxState[i].fx != Ifx_None); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2274 | if (d->fxState[i].fx == Ifx_Write) |
| 2275 | continue; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2276 | |
| 2277 | /* Ignore any sections marked as 'always defined'. */ |
| 2278 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) { |
| 2279 | if (0) |
| 2280 | VG_(printf)("memcheck: Dirty gst: ignored off %d, sz %d\n", |
| 2281 | d->fxState[i].offset, d->fxState[i].size ); |
| 2282 | continue; |
| 2283 | } |
| 2284 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2285 | /* This state element is read or modified. So we need to |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2286 | consider it. If larger than 8 bytes, deal with it in 8-byte |
| 2287 | chunks. */ |
| 2288 | gSz = d->fxState[i].size; |
| 2289 | gOff = d->fxState[i].offset; |
| 2290 | tl_assert(gSz > 0); |
| 2291 | while (True) { |
| 2292 | if (gSz == 0) break; |
| 2293 | n = gSz <= 8 ? gSz : 8; |
| 2294 | /* update 'curr' with UifU of the state slice |
| 2295 | gOff .. gOff+n-1 */ |
| 2296 | tySrc = szToITy( n ); |
| 2297 | src = assignNew( mce, tySrc, |
| 2298 | shadow_GET(mce, gOff, tySrc ) ); |
| 2299 | here = mkPCastTo( mce, Ity_I32, src ); |
| 2300 | curr = mkUifU32(mce, here, curr); |
| 2301 | gSz -= n; |
| 2302 | gOff += n; |
| 2303 | } |
| 2304 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | /* Inputs: memory. First set up some info needed regardless of |
| 2308 | whether we're doing reads or writes. */ |
| 2309 | tyAddr = Ity_INVALID; |
| 2310 | |
| 2311 | if (d->mFx != Ifx_None) { |
| 2312 | /* Because we may do multiple shadow loads/stores from the same |
| 2313 | base address, it's best to do a single test of its |
| 2314 | definedness right now. Post-instrumentation optimisation |
| 2315 | should remove all but this test. */ |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2316 | tl_assert(d->mAddr); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2317 | complainIfUndefined(mce, d->mAddr); |
| 2318 | |
| 2319 | tyAddr = typeOfIRExpr(mce->bb->tyenv, d->mAddr); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2320 | tl_assert(tyAddr == Ity_I32 || tyAddr == Ity_I64); |
| 2321 | tl_assert(tyAddr == mce->hWordTy); /* not really right */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | /* Deal with memory inputs (reads or modifies) */ |
| 2325 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) { |
| 2326 | offset = 0; |
| 2327 | toDo = d->mSize; |
| 2328 | /* chew off 32-bit chunks */ |
| 2329 | while (toDo >= 4) { |
| 2330 | here = mkPCastTo( |
| 2331 | mce, Ity_I32, |
| 2332 | expr2vbits_LDle ( mce, Ity_I32, |
| 2333 | d->mAddr, d->mSize - toDo ) |
| 2334 | ); |
| 2335 | curr = mkUifU32(mce, here, curr); |
| 2336 | toDo -= 4; |
| 2337 | } |
| 2338 | /* chew off 16-bit chunks */ |
| 2339 | while (toDo >= 2) { |
| 2340 | here = mkPCastTo( |
| 2341 | mce, Ity_I32, |
| 2342 | expr2vbits_LDle ( mce, Ity_I16, |
| 2343 | d->mAddr, d->mSize - toDo ) |
| 2344 | ); |
| 2345 | curr = mkUifU32(mce, here, curr); |
| 2346 | toDo -= 2; |
| 2347 | } |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2348 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /* Whew! So curr is a 32-bit V-value summarising pessimistically |
| 2352 | all the inputs to the helper. Now we need to re-distribute the |
| 2353 | results to all destinations. */ |
| 2354 | |
| 2355 | /* Outputs: the destination temporary, if there is one. */ |
sewardj | 92d168d | 2004-11-15 14:22:12 +0000 | [diff] [blame] | 2356 | if (d->tmp != IRTemp_INVALID) { |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2357 | dst = findShadowTmp(mce, d->tmp); |
| 2358 | tyDst = typeOfIRTemp(mce->bb->tyenv, d->tmp); |
| 2359 | assign( mce->bb, dst, mkPCastTo( mce, tyDst, curr) ); |
| 2360 | } |
| 2361 | |
| 2362 | /* Outputs: guest state that we write or modify. */ |
| 2363 | for (i = 0; i < d->nFxState; i++) { |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2364 | tl_assert(d->fxState[i].fx != Ifx_None); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2365 | if (d->fxState[i].fx == Ifx_Read) |
| 2366 | continue; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2367 | /* Ignore any sections marked as 'always defined'. */ |
| 2368 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) |
| 2369 | continue; |
| 2370 | /* This state element is written or modified. So we need to |
| 2371 | consider it. If larger than 8 bytes, deal with it in 8-byte |
| 2372 | chunks. */ |
| 2373 | gSz = d->fxState[i].size; |
| 2374 | gOff = d->fxState[i].offset; |
| 2375 | tl_assert(gSz > 0); |
| 2376 | while (True) { |
| 2377 | if (gSz == 0) break; |
| 2378 | n = gSz <= 8 ? gSz : 8; |
| 2379 | /* Write suitably-casted 'curr' to the state slice |
| 2380 | gOff .. gOff+n-1 */ |
| 2381 | tyDst = szToITy( n ); |
| 2382 | do_shadow_PUT( mce, gOff, |
| 2383 | NULL, /* original atom */ |
| 2384 | mkPCastTo( mce, tyDst, curr ) ); |
| 2385 | gSz -= n; |
| 2386 | gOff += n; |
| 2387 | } |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | /* Outputs: memory that we write or modify. */ |
| 2391 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) { |
| 2392 | offset = 0; |
| 2393 | toDo = d->mSize; |
| 2394 | /* chew off 32-bit chunks */ |
| 2395 | while (toDo >= 4) { |
| 2396 | do_shadow_STle( mce, d->mAddr, d->mSize - toDo, |
| 2397 | NULL, /* original data */ |
| 2398 | mkPCastTo( mce, Ity_I32, curr ) ); |
| 2399 | toDo -= 4; |
| 2400 | } |
| 2401 | /* chew off 16-bit chunks */ |
| 2402 | while (toDo >= 2) { |
| 2403 | do_shadow_STle( mce, d->mAddr, d->mSize - toDo, |
| 2404 | NULL, /* original data */ |
| 2405 | mkPCastTo( mce, Ity_I16, curr ) ); |
| 2406 | toDo -= 2; |
| 2407 | } |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2408 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | } |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 2412 | #endif /* UNUSED */ |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2413 | |
| 2414 | |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2415 | /*------------------------------------------------------------*/ |
| 2416 | /*--- Memcheck main ---*/ |
| 2417 | /*------------------------------------------------------------*/ |
| 2418 | |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2419 | #if 0 /* UNUSED */ |
| 2420 | static Bool isBogusAtom ( IRAtom* at ) |
| 2421 | { |
| 2422 | ULong n = 0; |
| 2423 | IRConst* con; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2424 | tl_assert(isIRAtom(at)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2425 | if (at->tag == Iex_Tmp) |
| 2426 | return False; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2427 | tl_assert(at->tag == Iex_Const); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2428 | con = at->Iex.Const.con; |
| 2429 | switch (con->tag) { |
| 2430 | case Ico_U8: n = (ULong)con->Ico.U8; break; |
| 2431 | case Ico_U16: n = (ULong)con->Ico.U16; break; |
| 2432 | case Ico_U32: n = (ULong)con->Ico.U32; break; |
| 2433 | case Ico_U64: n = (ULong)con->Ico.U64; break; |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2434 | default: ppIRExpr(at); tl_assert(0); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2435 | } |
| 2436 | /* VG_(printf)("%llx\n", n); */ |
| 2437 | return (n == 0xFEFEFEFF |
| 2438 | || n == 0x80808080 |
| 2439 | || n == 0x1010101 |
| 2440 | || n == 1010100); |
| 2441 | } |
| 2442 | |
| 2443 | static Bool checkForBogusLiterals ( /*FLAT*/ IRStmt* st ) |
| 2444 | { |
| 2445 | Int i; |
| 2446 | IRExpr* e; |
| 2447 | switch (st->tag) { |
| 2448 | case Ist_Tmp: |
| 2449 | e = st->Ist.Tmp.data; |
| 2450 | switch (e->tag) { |
| 2451 | case Iex_Get: |
| 2452 | case Iex_Tmp: |
| 2453 | return False; |
| 2454 | case Iex_Unop: |
| 2455 | return isBogusAtom(e->Iex.Unop.arg); |
| 2456 | case Iex_Binop: |
| 2457 | return isBogusAtom(e->Iex.Binop.arg1) |
| 2458 | || isBogusAtom(e->Iex.Binop.arg2); |
| 2459 | case Iex_Mux0X: |
| 2460 | return isBogusAtom(e->Iex.Mux0X.cond) |
| 2461 | || isBogusAtom(e->Iex.Mux0X.expr0) |
| 2462 | || isBogusAtom(e->Iex.Mux0X.exprX); |
| 2463 | case Iex_LDle: |
| 2464 | return isBogusAtom(e->Iex.LDle.addr); |
| 2465 | case Iex_CCall: |
| 2466 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 2467 | if (isBogusAtom(e->Iex.CCall.args[i])) |
| 2468 | return True; |
| 2469 | return False; |
| 2470 | default: |
| 2471 | goto unhandled; |
| 2472 | } |
| 2473 | case Ist_Put: |
| 2474 | return isBogusAtom(st->Ist.Put.data); |
| 2475 | case Ist_STle: |
| 2476 | return isBogusAtom(st->Ist.STle.addr) |
| 2477 | || isBogusAtom(st->Ist.STle.data); |
| 2478 | case Ist_Exit: |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2479 | return isBogusAtom(st->Ist.Exit.cond); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2480 | default: |
| 2481 | unhandled: |
| 2482 | ppIRStmt(st); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2483 | VG_(tool_panic)("hasBogusLiterals"); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | #endif /* UNUSED */ |
| 2487 | |
| 2488 | |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 2489 | #if 0 /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2490 | IRBB* mc_instrument ( IRBB* bb_in, VexGuestLayout* layout, IRType hWordTy ) |
| 2491 | { |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2492 | Bool verboze = False; //True; |
| 2493 | |
| 2494 | /* Bool hasBogusLiterals = False; */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2495 | |
| 2496 | Int i, j, first_stmt; |
| 2497 | IRStmt* st; |
| 2498 | MCEnv mce; |
| 2499 | |
| 2500 | /* Set up BB */ |
| 2501 | IRBB* bb = emptyIRBB(); |
| 2502 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 2503 | bb->next = dopyIRExpr(bb_in->next); |
| 2504 | bb->jumpkind = bb_in->jumpkind; |
| 2505 | |
| 2506 | /* Set up the running environment. Only .bb is modified as we go |
| 2507 | along. */ |
| 2508 | mce.bb = bb; |
| 2509 | mce.layout = layout; |
| 2510 | mce.n_originalTmps = bb->tyenv->types_used; |
| 2511 | mce.hWordTy = hWordTy; |
| 2512 | mce.tmpMap = LibVEX_Alloc(mce.n_originalTmps * sizeof(IRTemp)); |
| 2513 | for (i = 0; i < mce.n_originalTmps; i++) |
sewardj | 92d168d | 2004-11-15 14:22:12 +0000 | [diff] [blame] | 2514 | mce.tmpMap[i] = IRTemp_INVALID; |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2515 | |
| 2516 | /* Iterate over the stmts. */ |
| 2517 | |
| 2518 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 2519 | st = bb_in->stmts[i]; |
| 2520 | if (!st) continue; |
| 2521 | |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2522 | tl_assert(isFlatIRStmt(st)); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* |
| 2525 | if (!hasBogusLiterals) { |
| 2526 | hasBogusLiterals = checkForBogusLiterals(st); |
| 2527 | if (hasBogusLiterals) { |
| 2528 | VG_(printf)("bogus: "); |
| 2529 | ppIRStmt(st); |
| 2530 | VG_(printf)("\n"); |
| 2531 | } |
| 2532 | } |
| 2533 | */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2534 | first_stmt = bb->stmts_used; |
| 2535 | |
| 2536 | if (verboze) { |
| 2537 | ppIRStmt(st); |
| 2538 | VG_(printf)("\n\n"); |
| 2539 | } |
| 2540 | |
| 2541 | switch (st->tag) { |
| 2542 | |
| 2543 | case Ist_Tmp: |
| 2544 | assign( bb, findShadowTmp(&mce, st->Ist.Tmp.tmp), |
| 2545 | expr2vbits( &mce, st->Ist.Tmp.data) ); |
| 2546 | break; |
| 2547 | |
| 2548 | case Ist_Put: |
| 2549 | do_shadow_PUT( &mce, |
| 2550 | st->Ist.Put.offset, |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2551 | st->Ist.Put.data, |
| 2552 | NULL /* shadow atom */ ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2553 | break; |
| 2554 | |
| 2555 | case Ist_PutI: |
| 2556 | do_shadow_PUTI( &mce, |
| 2557 | st->Ist.PutI.descr, |
| 2558 | st->Ist.PutI.ix, |
| 2559 | st->Ist.PutI.bias, |
| 2560 | st->Ist.PutI.data ); |
| 2561 | break; |
| 2562 | |
| 2563 | case Ist_STle: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2564 | do_shadow_STle( &mce, st->Ist.STle.addr, 0/* addr bias */, |
| 2565 | st->Ist.STle.data, |
| 2566 | NULL /* shadow data */ ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | |
| 2569 | case Ist_Exit: |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2570 | /* if (!hasBogusLiterals) */ |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 2571 | complainIfUndefined( &mce, st->Ist.Exit.guard ); |
sewardj | 3598ef9 | 2004-11-11 02:13:30 +0000 | [diff] [blame] | 2572 | break; |
| 2573 | |
| 2574 | case Ist_Dirty: |
| 2575 | do_shadow_Dirty( &mce, st->Ist.Dirty.details ); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2576 | break; |
| 2577 | |
| 2578 | default: |
| 2579 | VG_(printf)("\n"); |
| 2580 | ppIRStmt(st); |
| 2581 | VG_(printf)("\n"); |
sewardj | 69f9e3e | 2004-12-30 02:05:34 +0000 | [diff] [blame] | 2582 | VG_(tool_panic)("memcheck: unhandled IRStmt"); |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2583 | |
| 2584 | } /* switch (st->tag) */ |
| 2585 | |
| 2586 | if (verboze) { |
| 2587 | for (j = first_stmt; j < bb->stmts_used; j++) { |
| 2588 | VG_(printf)(" "); |
| 2589 | ppIRStmt(bb->stmts[j]); |
| 2590 | VG_(printf)("\n"); |
| 2591 | } |
| 2592 | VG_(printf)("\n"); |
| 2593 | } |
| 2594 | |
| 2595 | addStmtToIRBB(bb, st); |
| 2596 | |
| 2597 | } |
| 2598 | |
| 2599 | /* Now we need to complain if the jump target is undefined. */ |
| 2600 | first_stmt = bb->stmts_used; |
| 2601 | |
| 2602 | if (verboze) { |
| 2603 | VG_(printf)("bb->next = "); |
| 2604 | ppIRExpr(bb->next); |
| 2605 | VG_(printf)("\n\n"); |
| 2606 | } |
| 2607 | |
| 2608 | complainIfUndefined( &mce, bb->next ); |
| 2609 | |
| 2610 | if (verboze) { |
| 2611 | for (j = first_stmt; j < bb->stmts_used; j++) { |
| 2612 | VG_(printf)(" "); |
| 2613 | ppIRStmt(bb->stmts[j]); |
| 2614 | VG_(printf)("\n"); |
| 2615 | } |
| 2616 | VG_(printf)("\n"); |
| 2617 | } |
| 2618 | |
| 2619 | return bb; |
| 2620 | } |
cerion | aee4524 | 2005-03-17 14:03:36 +0000 | [diff] [blame] | 2621 | #endif /* UNUSED */ |
sewardj | de8a5ae | 2004-11-06 14:20:54 +0000 | [diff] [blame] | 2622 | |
| 2623 | /*--------------------------------------------------------------------*/ |
| 2624 | /*--- end mc_translate.c ---*/ |
| 2625 | /*--------------------------------------------------------------------*/ |