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