Author: florian
Date: Thu Nov 20 15:08:56 2014
New Revision: 2998
Log:
This change was triggered by BZ #247974 which suggested to include
VEX/test_main.* in the tarball. We don't want to do that because those
files are really just scaffolding for developers to play with and not
meant for general consumption (and are also bitrotting ATM). Therefore,
this patch moves them to the "useful" subdirectory and adds a crude
Makefile there to build the executable.
Makefile-gcc updated accordingly.
Added:
trunk/useful/Makefile-vex
trunk/useful/test_main.c
- copied unchanged from r2997, trunk/test_main.c
trunk/useful/test_main.h
- copied unchanged from r2997, trunk/test_main.h
trunk/useful/test_main.h.base
- copied unchanged from r2997, trunk/test_main.h.base
Removed:
trunk/test_main.c
trunk/test_main.h
trunk/test_main.h.base
Modified:
trunk/Makefile-gcc
Modified: trunk/Makefile-gcc
==============================================================================
--- trunk/Makefile-gcc (original)
+++ trunk/Makefile-gcc Thu Nov 20 15:08:56 2014
@@ -131,8 +131,7 @@
scratch: clean all
-vex: libvex.a test_main.o
- $(CC) $(CCFLAGS) -o vex test_main.o libvex.a
+vex: libvex.a
libvex.a: $(LIB_OBJS)
rm -f libvex.a
@@ -206,15 +205,14 @@
clean:
- rm -f $(LIB_OBJS) *.a vex test_main.o TAG-* \
+ rm -f $(LIB_OBJS) *.a TAG-* \
pub/libvex_guest_offsets.h \
auxprogs/genoffsets.s
minidist:
rm -f vex--minidist-2005MMDD.tar
tar cf vex--minidist-2005MMDD.tar $(PUB_HEADERS) $(PRIV_HEADERS) \
- test_main.c test_main.h \
- Makefile \
+ Makefile-gcc \
`echo $(LIB_OBJS) | sed "s/\.o/\.c/g"`
@echo
@echo minidist done, size follows:
@@ -241,10 +239,6 @@
ALL_HEADERS = $(PUB_HEADERS) $(PRIV_HEADERS)
ALL_INCLUDES = $(PUB_INCLUDES) $(PRIV_INCLUDES)
-test_main.o: $(PUB_HEADERS) test_main.c test_main.h
- $(CC) $(CCFLAGS) $(PUB_INCLUDES) -o test_main.o \
- -c test_main.c
-
priv/ir_defs.o: $(ALL_HEADERS) priv/ir_defs.c
$(CC) $(CCFLAGS) $(ALL_INCLUDES) -o priv/ir_defs.o \
-c priv/ir_defs.c
Removed: trunk/test_main.c
==============================================================================
--- trunk/test_main.c (original)
+++ trunk/test_main.c (removed)
@@ -1,2734 +0,0 @@
-
-/*---------------------------------------------------------------*/
-/*--- begin test_main.c ---*/
-/*---------------------------------------------------------------*/
-
-/*
- This file is part of Valgrind, a dynamic binary instrumentation
- framework.
-
- Copyright (C) 2004-2013 OpenWorks LLP
- in...@op...
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA.
-
- The GNU General Public License is contained in the file COPYING.
-
- Neither the names of the U.S. Department of Energy nor the
- University of California nor the names of its contributors may be
- used to endorse or promote products derived from this software
- without prior written permission.
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <string.h>
-
-#include "libvex_basictypes.h"
-#include "libvex.h"
-
-#include "test_main.h"
-
-
-/*---------------------------------------------------------------*/
-/*--- Test ---*/
-/*---------------------------------------------------------------*/
-
-
-__attribute__ ((noreturn))
-static
-void failure_exit ( void )
-{
- fprintf(stdout, "VEX did failure_exit. Bye.\n");
- exit(1);
-}
-
-static
-void log_bytes ( HChar* bytes, Int nbytes )
-{
- fwrite ( bytes, 1, nbytes, stdout );
-}
-
-#define N_LINEBUF 10000
-static HChar linebuf[N_LINEBUF];
-
-#define N_ORIGBUF 10000
-#define N_TRANSBUF 5000
-
-static UChar origbuf[N_ORIGBUF];
-static UChar transbuf[N_TRANSBUF];
-
-static Bool verbose = True;
-
-/* Forwards */
-#if 1 /* UNUSED */
-//static IRSB* ac_instrument ( IRSB*, VexGuestLayout*, IRType );
-static
-IRSB* mc_instrument ( void* closureV,
- IRSB* bb_in, VexGuestLayout* layout,
- VexGuestExtents* vge,
- IRType gWordTy, IRType hWordTy );
-#endif
-
-static Bool chase_into_not_ok ( void* opaque, Addr64 dst ) {
- return False;
-}
-static UInt needs_self_check ( void* opaque, VexGuestExtents* vge ) {
- return 0;
-}
-
-int main ( int argc, char** argv )
-{
- FILE* f;
- Int i;
- UInt u, sum;
- Addr32 orig_addr;
- Int bb_number, n_bbs_done = 0;
- Int orig_nbytes, trans_used;
- VexTranslateResult tres;
- VexControl vcon;
- VexGuestExtents vge;
- VexArchInfo vai_x86, vai_amd64, vai_ppc32, vai_arm;
- VexAbiInfo vbi;
- VexTranslateArgs vta;
-
- if (argc != 2) {
- fprintf(stderr, "usage: vex file.orig\n");
- exit(1);
- }
- f = fopen(argv[1], "r");
- if (!f) {
- fprintf(stderr, "can't open `%s'\n", argv[1]);
- exit(1);
- }
-
- /* Run with default params. However, we can't allow bb chasing
- since that causes the front end to get segfaults when it tries
- to read code outside the initial BB we hand it. So when calling
- LibVEX_Translate, send in a chase-into predicate that always
- returns False. */
- LibVEX_default_VexControl ( &vcon );
- vcon.iropt_level = 2;
- vcon.guest_max_insns = 60;
-
- LibVEX_Init ( &failure_exit, &log_bytes,
- 1, /* debug_paranoia */
- &vcon );
-
-
- while (!feof(f)) {
-
- __attribute__((unused))
- char* unused1 = fgets(linebuf, N_LINEBUF,f);
- if (linebuf[0] == 0) continue;
- if (linebuf[0] != '.') continue;
-
- if (n_bbs_done == TEST_N_BBS) break;
- n_bbs_done++;
-
- /* first line is: . bb-number bb-addr n-bytes */
- assert(3 == sscanf(&linebuf[1], " %d %x %d\n",
- & bb_number,
- & orig_addr, & orig_nbytes ));
- assert(orig_nbytes >= 1);
- assert(!feof(f));
- __attribute__((unused))
- char* unused2 = fgets(linebuf, N_LINEBUF,f);
- assert(linebuf[0] == '.');
-
- /* second line is: . byte byte byte etc */
- if (verbose)
- printf("============ Basic Block %d, Done %d, "
- "Start %x, nbytes %2d ============",
- bb_number, n_bbs_done-1, orig_addr, orig_nbytes);
-
- /* thumb ITstate analysis needs to examine the 18 bytes
- preceding the first instruction. So let's leave the first 18
- zeroed out. */
- memset(origbuf, 0, sizeof(origbuf));
-
- assert(orig_nbytes >= 1 && orig_nbytes <= N_ORIGBUF);
- for (i = 0; i < orig_nbytes; i++) {
- assert(1 == sscanf(&linebuf[2 + 3*i], "%x", &u));
- origbuf[18+ i] = (UChar)u;
- }
-
- /* FIXME: put sensible values into the .hwcaps fields */
- LibVEX_default_VexArchInfo(&vai_x86);
- vai_x86.hwcaps = VEX_HWCAPS_X86_SSE1
- | VEX_HWCAPS_X86_SSE2 | VEX_HWCAPS_X86_SSE3;
-
- LibVEX_default_VexArchInfo(&vai_amd64);
- vai_amd64.hwcaps = 0;
-
- LibVEX_default_VexArchInfo(&vai_ppc32);
- vai_ppc32.hwcaps = 0;
- vai_ppc32.ppc_icache_line_szB = 128;
-
- LibVEX_default_VexArchInfo(&vai_arm);
- vai_arm.hwcaps = VEX_HWCAPS_ARM_VFP3 | VEX_HWCAPS_ARM_NEON | 7;
-
- LibVEX_default_VexAbiInfo(&vbi);
- vbi.guest_stack_redzone_size = 128;
-
- /* ----- Set up args for LibVEX_Translate ----- */
-
- vta.abiinfo_both = vbi;
- vta.guest_bytes = &origbuf[18];
- vta.guest_bytes_addr = (Addr64)orig_addr;
- vta.callback_opaque = NULL;
- vta.chase_into_ok = chase_into_not_ok;
- vta.guest_extents = &vge;
- vta.host_bytes = transbuf;
- vta.host_bytes_size = N_TRANSBUF;
- vta.host_bytes_used = &trans_used;
-
-#if 0 /* ppc32 -> ppc32 */
- vta.arch_guest = VexArchPPC32;
- vta.archinfo_guest = vai_ppc32;
- vta.arch_host = VexArchPPC32;
- vta.archinfo_host = vai_ppc32;
-#endif
-#if 0 /* amd64 -> amd64 */
- vta.arch_guest = VexArchAMD64;
- vta.archinfo_guest = vai_amd64;
- vta.arch_host = VexArchAMD64;
- vta.archinfo_host = vai_amd64;
-#endif
-#if 0 /* x86 -> x86 */
- vta.arch_guest = VexArchX86;
- vta.archinfo_guest = vai_x86;
- vta.arch_host = VexArchX86;
- vta.archinfo_host = vai_x86;
-#endif
-#if 1 /* arm -> arm */
- vta.arch_guest = VexArchARM;
- vta.archinfo_guest = vai_arm;
- vta.arch_host = VexArchARM;
- vta.archinfo_host = vai_arm;
- /* ARM/Thumb only hacks, that are needed to keep the ITstate
- analyser in the front end happy. */
- vta.guest_bytes = &origbuf[18 +1];
- vta.guest_bytes_addr = (Addr64)(&origbuf[18 +1]);
-#endif
-
-#if 1 /* no instrumentation */
- vta.instrument1 = NULL;
- vta.instrument2 = NULL;
-#endif
-#if 0 /* addrcheck */
- vta.instrument1 = ac_instrument;
- vta.instrument2 = NULL;
-#endif
-#if 0 /* memcheck */
- vta.instrument1 = mc_instrument;
- vta.instrument2 = NULL;
-#endif
- vta.needs_self_check = needs_self_check;
- vta.preamble_function = NULL;
- vta.traceflags = TEST_FLAGS;
- vta.addProfInc = False;
- vta.sigill_diag = True;
-
- vta.disp_cp_chain_me_to_slowEP = (void*)0x12345678;
- vta.disp_cp_chain_me_to_fastEP = (void*)0x12345679;
- vta.disp_cp_xindir = (void*)0x1234567A;
- vta.disp_cp_xassisted = (void*)0x1234567B;
-
- vta.finaltidy = NULL;
-
- for (i = 0; i < TEST_N_ITERS; i++)
- tres = LibVEX_Translate ( &vta );
-
- if (tres.status != VexTransOK)
- printf("\ntres = %d\n", (Int)tres.status);
- assert(tres.status == VexTransOK);
- assert(tres.n_sc_extents == 0);
- assert(vge.n_used == 1);
- assert((UInt)(vge.len[0]) == orig_nbytes);
-
- sum = 0;
- for (i = 0; i < trans_used; i++)
- sum += (UInt)transbuf[i];
- printf ( " %6.2f ... %u\n",
- (double)trans_used / (double)vge.len[0], sum );
- }
-
- fclose(f);
- printf("\n");
- LibVEX_ShowAllocStats();
-
- return 0;
-}
-
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-
-#if 0 /* UNUSED */
-
-static
-__attribute((noreturn))
-void panic ( HChar* s )
-{
- printf("\npanic: %s\n", s);
- failure_exit();
-}
-
-static
-IRSB* ac_instrument (IRSB* bb_in, VexGuestLayout* layout, IRType hWordTy )
-{
-/* Use this rather than eg. -1 because it's a UInt. */
-#define INVALID_DATA_SIZE 999999
-
- Int i;
- Int sz;
- IRCallee* helper;
- IRStmt* st;
- IRExpr* data;
- IRExpr* addr;
- Bool needSz;
-
- /* Set up BB */
- IRSB* bb = emptyIRSB();
- bb->tyenv = dopyIRTypeEnv(bb_in->tyenv);
- bb->next = dopyIRExpr(bb_in->next);
- bb->jumpkind = bb_in->jumpkind;
-
- /* No loads to consider in ->next. */
- assert(isIRAtom(bb_in->next));
-
- for (i = 0; i < bb_in->stmts_used; i++) {
- st = bb_in->stmts[i];
- if (!st) continue;
-
- switch (st->tag) {
-
- case Ist_Tmp:
- data = st->Ist.Tmp.data;
- if (data->tag == Iex_LDle) {
- addr = data->Iex.LDle.addr;
- sz = sizeofIRType(data->Iex.LDle.ty);
- needSz = False;
- switch (sz) {
- case 4: helper = mkIRCallee(1, "ac_helperc_LOAD4",
- (void*)0x12345601); break;
- case 2: helper = mkIRCallee(0, "ac_helperc_LOAD2",
- (void*)0x12345602); break;
- case 1: helper = mkIRCallee(1, "ac_helperc_LOAD1",
- (void*)0x12345603); break;
- default: helper = mkIRCallee(0, "ac_helperc_LOADN",
- (void*)0x12345604);
- needSz = True; break;
- }
- if (needSz) {
- addStmtToIRSB(
- bb,
- IRStmt_Dirty(
- unsafeIRDirty_0_N( helper->regparms,
- helper->name, helper->addr,
- mkIRExprVec_2(addr, mkIRExpr_HWord(sz)))
- ));
- } else {
- addStmtToIRSB(
- bb,
- IRStmt_Dirty(
- unsafeIRDirty_0_N( helper->regparms,
- helper->name, helper->addr,
- mkIRExprVec_1(addr) )
- ));
- }
- }
- break;
-
- case Ist_STle:
- data = st->Ist.STle.data;
- addr = st->Ist.STle.addr;
- assert(isIRAtom(data));
- assert(isIRAtom(addr));
- sz = sizeofIRType(typeOfIRExpr(bb_in->tyenv, data));
- needSz = False;
- switch (sz) {
- case 4: helper = mkIRCallee(1, "ac_helperc_STORE4",
- (void*)0x12345605); break;
- case 2: helper = mkIRCallee(0, "ac_helperc_STORE2",
- (void*)0x12345606); break;
- case 1: helper = mkIRCallee(1, "ac_helperc_STORE1",
- (void*)0x12345607); break;
- default: helper = mkIRCallee(0, "ac_helperc_STOREN",
- (void*)0x12345608);
- needSz = True; break;
- }
- if (needSz) {
- addStmtToIRSB(
- bb,
- IRStmt_Dirty(
- unsafeIRDirty_0_N( helper->regparms,
- helper->name, helper->addr,
- mkIRExprVec_2(addr, mkIRExpr_HWord(sz)))
- ));
- } else {
- addStmtToIRSB(
- bb,
- IRStmt_Dirty(
- unsafeIRDirty_0_N( helper->regparms,
- helper->name, helper->addr,
- mkIRExprVec_1(addr) )
- ));
- }
- break;
-
- case Ist_Put:
- assert(isIRAtom(st->Ist.Put.data));
- break;
-
- case Ist_PutI:
- assert(isIRAtom(st->Ist.PutI.ix));
- assert(isIRAtom(st->Ist.PutI.data));
- break;
-
- case Ist_Exit:
- assert(isIRAtom(st->Ist.Exit.guard));
- break;
-
- case Ist_Dirty:
- /* If the call doesn't interact with memory, we ain't
- interested. */
- if (st->Ist.Dirty.details->mFx == Ifx_None)
- break;
- goto unhandled;
-
- default:
- unhandled:
- printf("\n");
- ppIRStmt(st);
- printf("\n");
- panic("addrcheck: unhandled IRStmt");
- }
-
- addStmtToIRSB( bb, dopyIRStmt(st));
- }
-
- return bb;
-}
-#endif /* UNUSED */
-
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-
-#if 1 /* UNUSED */
-
-static
-__attribute((noreturn))
-void panic ( HChar* s )
-{
- printf("\npanic: %s\n", s);
- failure_exit();
-}
-
-#define tl_assert(xxx) assert(xxx)
-#define VG_(xxxx) xxxx
-#define tool_panic(zzz) panic(zzz)
-#define MC_(zzzz) MC_##zzzz
-#define TL_(zzzz) SK_##zzzz
-
-
-static void MC_helperc_complain_undef ( void );
-static void MC_helperc_LOADV8 ( void );
-static void MC_helperc_LOADV4 ( void );
-static void MC_helperc_LOADV2 ( void );
-static void MC_helperc_LOADV1 ( void );
-static void MC_helperc_STOREV8( void );
-static void MC_helperc_STOREV4( void );
-static void MC_helperc_STOREV2( void );
-static void MC_helperc_STOREV1( void );
-static void MC_helperc_value_check0_fail( void );
-static void MC_helperc_value_check1_fail( void );
-static void MC_helperc_value_check4_fail( void );
-
-static void MC_helperc_complain_undef ( void ) { }
-static void MC_helperc_LOADV8 ( void ) { }
-static void MC_helperc_LOADV4 ( void ) { }
-static void MC_helperc_LOADV2 ( void ) { }
-static void MC_helperc_LOADV1 ( void ) { }
-static void MC_helperc_STOREV8( void ) { }
-static void MC_helperc_STOREV4( void ) { }
-static void MC_helperc_STOREV2( void ) { }
-static void MC_helperc_STOREV1( void ) { }
-static void MC_helperc_value_check0_fail( void ) { }
-static void MC_helperc_value_check1_fail( void ) { }
-static void MC_helperc_value_check4_fail( void ) { }
-
-
-/*--------------------------------------------------------------------*/
-/*--- Instrument IR to perform memory checking operations. ---*/
-/*--- mc_translate.c ---*/
-/*--------------------------------------------------------------------*/
-
-/*
- This file is part of MemCheck, a heavyweight Valgrind tool for
- detecting memory errors.
-
- Copyright (C) 2000-2013 Julian Seward
- js...@ac...
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307, USA.
-
- The GNU General Public License is contained in the file COPYING.
-*/
-
-//#include "mc_include.h"
-
-
-/*------------------------------------------------------------*/
-/*--- Forward decls ---*/
-/*------------------------------------------------------------*/
-
-struct _MCEnv;
-
-static IRType shadowType ( IRType ty );
-static IRExpr* expr2vbits ( struct _MCEnv* mce, IRExpr* e );
-
-
-/*------------------------------------------------------------*/
-/*--- Memcheck running state, and tmp management. ---*/
-/*------------------------------------------------------------*/
-
-/* Carries around state during memcheck instrumentation. */
-typedef
- struct _MCEnv {
- /* MODIFIED: the bb being constructed. IRStmts are added. */
- IRSB* bb;
-
- /* MODIFIED: a table [0 .. #temps_in_original_bb-1] which maps
- original temps to their current their current shadow temp.
- Initially all entries are IRTemp_INVALID. Entries are added
- lazily since many original temps are not used due to
- optimisation prior to instrumentation. Note that floating
- point original tmps are shadowed by integer tmps of the same
- size, and Bit-typed original tmps are shadowed by the type
- Ity_I8. See comment below. */
- IRTemp* tmpMap;
- Int n_originalTmps; /* for range checking */
-
- /* READONLY: the guest layout. This indicates which parts of
- the guest state should be regarded as 'always defined'. */
- VexGuestLayout* layout;
- /* READONLY: the host word type. Needed for constructing
- arguments of type 'HWord' to be passed to helper functions.
- Ity_I32 or Ity_I64 only. */
- IRType hWordTy;
- }
- MCEnv;
-
-/* SHADOW TMP MANAGEMENT. Shadow tmps are allocated lazily (on
- demand), as they are encountered. This is for two reasons.
-
- (1) (less important reason): Many original tmps are unused due to
- initial IR optimisation, and we do not want to spaces in tables
- tracking them.
-
- Shadow IRTemps are therefore allocated on demand. mce.tmpMap is a
- table indexed [0 .. n_types-1], which gives the current shadow for
- each original tmp, or INVALID_IRTEMP if none is so far assigned.
- It is necessary to support making multiple assignments to a shadow
- -- specifically, after testing a shadow for definedness, it needs
- to be made defined. But IR's SSA property disallows this.
-
- (2) (more important reason): Therefore, when a shadow needs to get
- a new value, a new temporary is created, the value is assigned to
- that, and the tmpMap is updated to reflect the new binding.
-
- A corollary is that if the tmpMap maps a given tmp to
- INVALID_IRTEMP and we are hoping to read that shadow tmp, it means
- there's a read-before-write error in the original tmps. The IR
- sanity checker should catch all such anomalies, however.
-*/
-
-/* Find the tmp currently shadowing the given original tmp. If none
- so far exists, allocate one. */
-static IRTemp findShadowTmp ( MCEnv* mce, IRTemp orig )
-{
- tl_assert(orig < mce->n_originalTmps);
- if (mce->tmpMap[orig] == IRTemp_INVALID) {
- mce->tmpMap[orig]
- = newIRTemp(mce->bb->tyenv,
- shadowType(mce->bb->tyenv->types[orig]));
- }
- return mce->tmpMap[orig];
-}
-
-/* Allocate a new shadow for the given original tmp. This means any
- previous shadow is abandoned. This is needed because it is
- necessary to give a new value to a shadow once it has been tested
- for undefinedness, but unfortunately IR's SSA property disallows
- this. Instead we must abandon the old shadow, allocate a new one
- and use that instead. */
-static void newShadowTmp ( MCEnv* mce, IRTemp orig )
-{
- tl_assert(orig < mce->n_originalTmps);
- mce->tmpMap[orig]
- = newIRTemp(mce->bb->tyenv,
- shadowType(mce->bb->tyenv->types[orig]));
-}
-
-
-/*------------------------------------------------------------*/
-/*--- IRAtoms -- a subset of IRExprs ---*/
-/*------------------------------------------------------------*/
-
-/* An atom is either an IRExpr_Const or an IRExpr_Tmp, as defined by
- isIRAtom() in libvex_ir.h. Because this instrumenter expects flat
- input, most of this code deals in atoms. Usefully, a value atom
- always has a V-value which is also an atom: constants are shadowed
- by constants, and temps are shadowed by the corresponding shadow
- temporary. */
-
-typedef IRExpr IRAtom;
-
-/* (used for sanity checks only): is this an atom which looks
- like it's from original code? */
-static Bool isOriginalAtom ( MCEnv* mce, IRAtom* a1 )
-{
- if (a1->tag == Iex_Const)
- return True;
- if (a1->tag == Iex_RdTmp && a1->Iex.RdTmp.tmp < mce->n_originalTmps)
- return True;
- return False;
-}
-
-/* (used for sanity checks only): is this an atom which looks
- like it's from shadow code? */
-static Bool isShadowAtom ( MCEnv* mce, IRAtom* a1 )
-{
- if (a1->tag == Iex_Const)
- return True;
- if (a1->tag == Iex_RdTmp && a1->Iex.RdTmp.tmp >= mce->n_originalTmps)
- return True;
- return False;
-}
-
-/* (used for sanity checks only): check that both args are atoms and
- are identically-kinded. */
-static Bool sameKindedAtoms ( IRAtom* a1, IRAtom* a2 )
-{
- if (a1->tag == Iex_RdTmp && a1->tag == Iex_RdTmp)
- return True;
- if (a1->tag == Iex_Const && a1->tag == Iex_Const)
- return True;
- return False;
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Type management ---*/
-/*------------------------------------------------------------*/
-
-/* Shadow state is always accessed using integer types. This returns
- an integer type with the same size (as per sizeofIRType) as the
- given type. The only valid shadow types are Bit, I8, I16, I32,
- I64, V128. */
-
-static IRType shadowType ( IRType ty )
-{
- switch (ty) {
- case Ity_I1:
- case Ity_I8:
- case Ity_I16:
- case Ity_I32:
- case Ity_I64: return ty;
- case Ity_F32: return Ity_I32;
- case Ity_F64: return Ity_I64;
- case Ity_V128: return Ity_V128;
- default: ppIRType(ty);
- VG_(tool_panic)("memcheck:shadowType");
- }
-}
-
-/* Produce a 'defined' value of the given shadow type. Should only be
- supplied shadow types (Bit/I8/I16/I32/UI64). */
-static IRExpr* definedOfType ( IRType ty ) {
- switch (ty) {
- case Ity_I1: return IRExpr_Const(IRConst_U1(False));
- case Ity_I8: return IRExpr_Const(IRConst_U8(0));
- case Ity_I16: return IRExpr_Const(IRConst_U16(0));
- case Ity_I32: return IRExpr_Const(IRConst_U32(0));
- case Ity_I64: return IRExpr_Const(IRConst_U64(0));
- case Ity_V128: return IRExpr_Const(IRConst_V128(0x0000));
- default: VG_(tool_panic)("memcheck:definedOfType");
- }
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Constructing IR fragments ---*/
-/*------------------------------------------------------------*/
-
-/* assign value to tmp */
-#define assign(_bb,_tmp,_expr) \
- addStmtToIRSB((_bb), IRStmt_WrTmp((_tmp),(_expr)))
-
-/* add stmt to a bb */
-#define stmt(_bb,_stmt) \
- addStmtToIRSB((_bb), (_stmt))
-
-/* build various kinds of expressions */
-#define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2))
-#define unop(_op, _arg) IRExpr_Unop((_op),(_arg))
-#define mkU8(_n) IRExpr_Const(IRConst_U8(_n))
-#define mkU16(_n) IRExpr_Const(IRConst_U16(_n))
-#define mkU32(_n) IRExpr_Const(IRConst_U32(_n))
-#define mkU64(_n) IRExpr_Const(IRConst_U64(_n))
-#define mkV128(_n) IRExpr_Const(IRConst_V128(_n))
-#define mkexpr(_tmp) IRExpr_RdTmp((_tmp))
-
-/* bind the given expression to a new temporary, and return the
- temporary. This effectively converts an arbitrary expression into
- an atom. */
-static IRAtom* assignNew ( MCEnv* mce, IRType ty, IRExpr* e ) {
- IRTemp t = newIRTemp(mce->bb->tyenv, ty);
- assign(mce->bb, t, e);
- return mkexpr(t);
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Constructing definedness primitive ops ---*/
-/*------------------------------------------------------------*/
-
-/* --------- Defined-if-either-defined --------- */
-
-static IRAtom* mkDifD8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I8, binop(Iop_And8, a1, a2));
-}
-
-static IRAtom* mkDifD16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I16, binop(Iop_And16, a1, a2));
-}
-
-static IRAtom* mkDifD32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I32, binop(Iop_And32, a1, a2));
-}
-
-static IRAtom* mkDifD64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I64, binop(Iop_And64, a1, a2));
-}
-
-static IRAtom* mkDifDV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_V128, binop(Iop_AndV128, a1, a2));
-}
-
-/* --------- Undefined-if-either-undefined --------- */
-
-static IRAtom* mkUifU8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I8, binop(Iop_Or8, a1, a2));
-}
-
-static IRAtom* mkUifU16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I16, binop(Iop_Or16, a1, a2));
-}
-
-static IRAtom* mkUifU32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I32, binop(Iop_Or32, a1, a2));
-}
-
-static IRAtom* mkUifU64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_I64, binop(Iop_Or64, a1, a2));
-}
-
-static IRAtom* mkUifUV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) {
- tl_assert(isShadowAtom(mce,a1));
- tl_assert(isShadowAtom(mce,a2));
- return assignNew(mce, Ity_V128, binop(Iop_OrV128, a1, a2));
-}
-
-static IRAtom* mkUifU ( MCEnv* mce, IRType vty, IRAtom* a1, IRAtom* a2 ) {
- switch (vty) {
- case Ity_I8: return mkUifU8(mce, a1, a2);
- case Ity_I16: return mkUifU16(mce, a1, a2);
- case Ity_I32: return mkUifU32(mce, a1, a2);
- case Ity_I64: return mkUifU64(mce, a1, a2);
- case Ity_V128: return mkUifUV128(mce, a1, a2);
- default:
- VG_(printf)("\n"); ppIRType(vty); VG_(printf)("\n");
- VG_(tool_panic)("memcheck:mkUifU");
- }
-}
-
-/* --------- The Left-family of operations. --------- */
-
-static IRAtom* mkLeft8 ( MCEnv* mce, IRAtom* a1 ) {
- tl_assert(isShadowAtom(mce,a1));
- /* It's safe to duplicate a1 since it's only an atom */
- return assignNew(mce, Ity_I8,
- binop(Iop_Or8, a1,
- assignNew(mce, Ity_I8,
- /* unop(Iop_Neg8, a1)))); */
- binop(Iop_Sub8, mkU8(0), a1) )));
-}
-
-static IRAtom* mkLeft16 ( MCEnv* mce, IRAtom* a1 ) {
- tl_assert(isShadowAtom(mce,a1));
- /* It's safe to duplicate a1 since it's only an atom */
- return assignNew(mce, Ity_I16,
- binop(Iop_Or16, a1,
- assignNew(mce, Ity_I16,
- /* unop(Iop_Neg16, a1)))); */
- binop(Iop_Sub16, mkU16(0), a1) )));
-}
-
-static IRAtom* mkLeft32 ( MCEnv* mce, IRAtom* a1 ) {
- tl_assert(isShadowAtom(mce,a1));
- /* It's safe to duplicate a1 since it's only an atom */
- return assignNew(mce, Ity_I32,
- binop(Iop_Or32, a1,
- assignNew(mce, Ity_I32,
- /* unop(Iop_Neg32, a1)))); */
- binop(Iop_Sub32, mkU32(0), a1) )));
-}
-
-/* --------- 'Improvement' functions for AND/OR. --------- */
-
-/* ImproveAND(data, vbits) = data OR vbits. Defined (0) data 0s give
- defined (0); all other -> undefined (1).
-*/
-static IRAtom* mkImproveAND8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(mce, Ity_I8, binop(Iop_Or8, data, vbits));
-}
-
-static IRAtom* mkImproveAND16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(mce, Ity_I16, binop(Iop_Or16, data, vbits));
-}
-
-static IRAtom* mkImproveAND32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(mce, Ity_I32, binop(Iop_Or32, data, vbits));
-}
-
-static IRAtom* mkImproveAND64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(mce, Ity_I64, binop(Iop_Or64, data, vbits));
-}
-
-static IRAtom* mkImproveANDV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(mce, Ity_V128, binop(Iop_OrV128, data, vbits));
-}
-
-/* ImproveOR(data, vbits) = ~data OR vbits. Defined (0) data 1s give
- defined (0); all other -> undefined (1).
-*/
-static IRAtom* mkImproveOR8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(
- mce, Ity_I8,
- binop(Iop_Or8,
- assignNew(mce, Ity_I8, unop(Iop_Not8, data)),
- vbits) );
-}
-
-static IRAtom* mkImproveOR16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(
- mce, Ity_I16,
- binop(Iop_Or16,
- assignNew(mce, Ity_I16, unop(Iop_Not16, data)),
- vbits) );
-}
-
-static IRAtom* mkImproveOR32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(
- mce, Ity_I32,
- binop(Iop_Or32,
- assignNew(mce, Ity_I32, unop(Iop_Not32, data)),
- vbits) );
-}
-
-static IRAtom* mkImproveOR64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(
- mce, Ity_I64,
- binop(Iop_Or64,
- assignNew(mce, Ity_I64, unop(Iop_Not64, data)),
- vbits) );
-}
-
-static IRAtom* mkImproveORV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits )
-{
- tl_assert(isOriginalAtom(mce, data));
- tl_assert(isShadowAtom(mce, vbits));
- tl_assert(sameKindedAtoms(data, vbits));
- return assignNew(
- mce, Ity_V128,
- binop(Iop_OrV128,
- assignNew(mce, Ity_V128, unop(Iop_NotV128, data)),
- vbits) );
-}
-
-/* --------- Pessimising casts. --------- */
-
-static IRAtom* mkPCastTo( MCEnv* mce, IRType dst_ty, IRAtom* vbits )
-{
- IRType ty;
- IRAtom* tmp1;
- /* Note, dst_ty is a shadow type, not an original type. */
- /* First of all, collapse vbits down to a single bit. */
- tl_assert(isShadowAtom(mce,vbits));
- ty = typeOfIRExpr(mce->bb->tyenv, vbits);
- tmp1 = NULL;
- switch (ty) {
- case Ity_I1:
- tmp1 = vbits;
- break;
- case Ity_I8:
- tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE8, vbits, mkU8(0)));
- break;
- case Ity_I16:
- tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE16, vbits, mkU16(0)));
- break;
- case Ity_I32:
- tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE32, vbits, mkU32(0)));
- break;
- case Ity_I64:
- tmp1 = assignNew(mce, Ity_I1, binop(Iop_CmpNE64, vbits, mkU64(0)));
- break;
- default:
- VG_(tool_panic)("mkPCastTo(1)");
- }
- tl_assert(tmp1);
- /* Now widen up to the dst type. */
- switch (dst_ty) {
- case Ity_I1:
- return tmp1;
- case Ity_I8:
- return assignNew(mce, Ity_I8, unop(Iop_1Sto8, tmp1));
- case Ity_I16:
- return assignNew(mce, Ity_I16, unop(Iop_1Sto16, tmp1));
- case Ity_I32:
- return assignNew(mce, Ity_I32, unop(Iop_1Sto32, tmp1));
- case Ity_I64:
- return assignNew(mce, Ity_I64, unop(Iop_1Sto64, tmp1));
- case Ity_V128:
- tmp1 = assignNew(mce, Ity_I64, unop(Iop_1Sto64, tmp1));
- tmp1 = assignNew(mce, Ity_V128, binop(Iop_64HLtoV128, tmp1, tmp1));
- return tmp1;
- default:
- ppIRType(dst_ty);
- VG_(tool_panic)("mkPCastTo(2)");
- }
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Emit a test and complaint if something is undefined. ---*/
-/*------------------------------------------------------------*/
-
-/* Set the annotations on a dirty helper to indicate that the stack
- pointer and instruction pointers might be read. This is the
- behaviour of all 'emit-a-complaint' style functions we might
- call. */
-
-static void setHelperAnns ( MCEnv* mce, IRDirty* di ) {
- di->nFxState = 2;
- di->fxState[0].fx = Ifx_Read;
- di->fxState[0].offset = mce->layout->offset_SP;
- di->fxState[0].size = mce->layout->sizeof_SP;
- di->fxState[1].fx = Ifx_Read;
- di->fxState[1].offset = mce->layout->offset_IP;
- di->fxState[1].size = mce->layout->sizeof_IP;
-}
-
-
-/* Check the supplied **original** atom for undefinedness, and emit a
- complaint if so. Once that happens, mark it as defined. This is
- possible because the atom is either a tmp or literal. If it's a
- tmp, it will be shadowed by a tmp, and so we can set the shadow to
- be defined. In fact as mentioned above, we will have to allocate a
- new tmp to carry the new 'defined' shadow value, and update the
- original->tmp mapping accordingly; we cannot simply assign a new
- value to an existing shadow tmp as this breaks SSAness -- resulting
- in the post-instrumentation sanity checker spluttering in disapproval.
-*/
-static void complainIfUndefined ( MCEnv* mce, IRAtom* atom )
-{
- IRAtom* vatom;
- IRType ty;
- Int sz;
- IRDirty* di;
- IRAtom* cond;
-
- /* Since the original expression is atomic, there's no duplicated
- work generated by making multiple V-expressions for it. So we
- don't really care about the possibility that someone else may
- also create a V-interpretion for it. */
- tl_assert(isOriginalAtom(mce, atom));
- vatom = expr2vbits( mce, atom );
- tl_assert(isShadowAtom(mce, vatom));
- tl_assert(sameKindedAtoms(atom, vatom));
-
- ty = typeOfIRExpr(mce->bb->tyenv, vatom);
-
- /* sz is only used for constructing the error message */
- sz = ty==Ity_I1 ? 0 : sizeofIRType(ty);
-
- cond = mkPCastTo( mce, Ity_I1, vatom );
- /* cond will be 0 if all defined, and 1 if any not defined. */
-
- switch (sz) {
- case 0:
- di = unsafeIRDirty_0_N( 0/*regparms*/,
- "MC_(helperc_value_check0_fail)",
- &MC_(helperc_value_check0_fail),
- mkIRExprVec_0()
- );
- break;
- case 1:
- di = unsafeIRDirty_0_N( 0/*regparms*/,
- "MC_(helperc_value_check1_fail)",
- &MC_(helperc_value_check1_fail),
- mkIRExprVec_0()
- );
- break;
- case 4:
- di = unsafeIRDirty_0_N( 0/*regparms*/,
- "MC_(helperc_value_check4_fail)",
- &MC_(helperc_value_check4_fail),
- mkIRExprVec_0()
- );
- break;
- default:
- di = unsafeIRDirty_0_N( 1/*regparms*/,
- "MC_(helperc_complain_undef)",
- &MC_(helperc_complain_undef),
- mkIRExprVec_1( mkIRExpr_HWord( sz ))
- );
- break;
- }
- di->guard = cond;
- setHelperAnns( mce, di );
- stmt( mce->bb, IRStmt_Dirty(di));
-
- /* Set the shadow tmp to be defined. First, update the
- orig->shadow tmp mapping to reflect the fact that this shadow is
- getting a new value. */
- tl_assert(isIRAtom(vatom));
- /* sameKindedAtoms ... */
- if (vatom->tag == Iex_RdTmp) {
- tl_assert(atom->tag == Iex_RdTmp);
- newShadowTmp(mce, atom->Iex.RdTmp.tmp);
- assign(mce->bb, findShadowTmp(mce, atom->Iex.RdTmp.tmp),
- definedOfType(ty));
- }
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Shadowing PUTs/GETs, and indexed variants thereof ---*/
-/*------------------------------------------------------------*/
-
-/* Examine the always-defined sections declared in layout to see if
- the (offset,size) section is within one. Note, is is an error to
- partially fall into such a region: (offset,size) should either be
- completely in such a region or completely not-in such a region.
-*/
-static Bool isAlwaysDefd ( MCEnv* mce, Int offset, Int size )
-{
- Int minoffD, maxoffD, i;
- Int minoff = offset;
- Int maxoff = minoff + size - 1;
- tl_assert((minoff & ~0xFFFF) == 0);
- tl_assert((maxoff & ~0xFFFF) == 0);
-
- for (i = 0; i < mce->layout->n_alwaysDefd; i++) {
- minoffD = mce->layout->alwaysDefd[i].offset;
- maxoffD = minoffD + mce->layout->alwaysDefd[i].size - 1;
- tl_assert((minoffD & ~0xFFFF) == 0);
- tl_assert((maxoffD & ~0xFFFF) == 0);
-
- if (maxoff < minoffD || maxoffD < minoff)
- continue; /* no overlap */
- if (minoff >= minoffD && maxoff <= maxoffD)
- return True; /* completely contained in an always-defd section */
-
- VG_(tool_panic)("memcheck:isAlwaysDefd:partial overlap");
- }
- return False; /* could not find any containing section */
-}
-
-
-/* Generate into bb suitable actions to shadow this Put. If the state
- slice is marked 'always defined', do nothing. Otherwise, write the
- supplied V bits to the shadow state. We can pass in either an
- original atom or a V-atom, but not both. In the former case the
- relevant V-bits are then generated from the original.
-*/
-static
-void do_shadow_PUT ( MCEnv* mce, Int offset,
- IRAtom* atom, IRAtom* vatom )
-{
- IRType ty;
- if (atom) {
- tl_assert(!vatom);
- tl_assert(isOriginalAtom(mce, atom));
- vatom = expr2vbits( mce, atom );
- } else {
- tl_assert(vatom);
- tl_assert(isShadowAtom(mce, vatom));
- }
-
- ty = typeOfIRExpr(mce->bb->tyenv, vatom);
- tl_assert(ty != Ity_I1);
- if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) {
- /* later: no ... */
- /* emit code to emit a complaint if any of the vbits are 1. */
- /* complainIfUndefined(mce, atom); */
- } else {
- /* Do a plain shadow Put. */
- stmt( mce->bb, IRStmt_Put( offset + mce->layout->total_sizeB, vatom ) );
- }
-}
-
-
-/* Return an expression which contains the V bits corresponding to the
- given GETI (passed in in pieces).
-*/
-static
-void do_shadow_PUTI ( MCEnv* mce,
- IRRegArray* descr, IRAtom* ix, Int bias, IRAtom* atom )
-{
- IRAtom* vatom;
- IRType ty, tyS;
- Int arrSize;;
-
- tl_assert(isOriginalAtom(mce,atom));
- vatom = expr2vbits( mce, atom );
- tl_assert(sameKindedAtoms(atom, vatom));
- ty = descr->elemTy;
- tyS = shadowType(ty);
- arrSize = descr->nElems * sizeofIRType(ty);
- tl_assert(ty != Ity_I1);
- tl_assert(isOriginalAtom(mce,ix));
- complainIfUndefined(mce,ix);
- if (isAlwaysDefd(mce, descr->base, arrSize)) {
- /* later: no ... */
- /* emit code to emit a complaint if any of the vbits are 1. */
- /* complainIfUndefined(mce, atom); */
- } else {
- /* Do a cloned version of the Put that refers to the shadow
- area. */
- IRRegArray* new_descr
- = mkIRRegArray( descr->base + mce->layout->total_sizeB,
- tyS, descr->nElems);
- stmt( mce->bb, IRStmt_PutI( mkIRPutI( new_descr, ix, bias, vatom ) ));
- }
-}
-
-
-/* Return an expression which contains the V bits corresponding to the
- given GET (passed in in pieces).
-*/
-static
-IRExpr* shadow_GET ( MCEnv* mce, Int offset, IRType ty )
-{
- IRType tyS = shadowType(ty);
- tl_assert(ty != Ity_I1);
- if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) {
- /* Always defined, return all zeroes of the relevant type */
- return definedOfType(tyS);
- } else {
- /* return a cloned version of the Get that refers to the shadow
- area. */
- return IRExpr_Get( offset + mce->layout->total_sizeB, tyS );
- }
-}
-
-
-/* Return an expression which contains the V bits corresponding to the
- given GETI (passed in in pieces).
-*/
-static
-IRExpr* shadow_GETI ( MCEnv* mce, IRRegArray* descr, IRAtom* ix, Int bias )
-{
- IRType ty = descr->elemTy;
- IRType tyS = shadowType(ty);
- Int arrSize = descr->nElems * sizeofIRType(ty);
- tl_assert(ty != Ity_I1);
- tl_assert(isOriginalAtom(mce,ix));
- complainIfUndefined(mce,ix);
- if (isAlwaysDefd(mce, descr->base, arrSize)) {
- /* Always defined, return all zeroes of the relevant type */
- return definedOfType(tyS);
- } else {
- /* return a cloned version of the Get that refers to the shadow
- area. */
- IRRegArray* new_descr
- = mkIRRegArray( descr->base + mce->layout->total_sizeB,
- tyS, descr->nElems);
- return IRExpr_GetI( new_descr, ix, bias );
- }
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Generating approximations for unknown operations, ---*/
-/*--- using lazy-propagate semantics ---*/
-/*------------------------------------------------------------*/
-
-/* Lazy propagation of undefinedness from two values, resulting in the
- specified shadow type.
-*/
-static
-IRAtom* mkLazy2 ( MCEnv* mce, IRType finalVty, IRAtom* va1, IRAtom* va2 )
-{
- /* force everything via 32-bit intermediaries. */
- IRAtom* at;
- tl_assert(isShadowAtom(mce,va1));
- tl_assert(isShadowAtom(mce,va2));
- at = mkPCastTo(mce, Ity_I32, va1);
- at = mkUifU(mce, Ity_I32, at, mkPCastTo(mce, Ity_I32, va2));
- at = mkPCastTo(mce, finalVty, at);
- return at;
-}
-
-
-/* Do the lazy propagation game from a null-terminated vector of
- atoms. This is presumably the arguments to a helper call, so the
- IRCallee info is also supplied in order that we can know which
- arguments should be ignored (via the .mcx_mask field).
-*/
-static
-IRAtom* mkLazyN ( MCEnv* mce,
- IRAtom** exprvec, IRType finalVtype, IRCallee* cee )
-{
- Int i;
- IRAtom* here;
- IRAtom* curr = definedOfType(Ity_I32);
- for (i = 0; exprvec[i]; i++) {
- tl_assert(i < 32);
- tl_assert(isOriginalAtom(mce, exprvec[i]));
- /* Only take notice of this arg if the callee's mc-exclusion
- mask does not say it is to be excluded. */
- if (cee->mcx_mask & (1<<i)) {
- /* the arg is to be excluded from definedness checking. Do
- nothing. */
- if (0) VG_(printf)("excluding %s(%d)\n", cee->name, i);
- } else {
- /* calculate the arg's definedness, and pessimistically merge
- it in. */
- here = mkPCastTo( mce, Ity_I32, expr2vbits(mce, exprvec[i]) );
- curr = mkUifU32(mce, here, curr);
- }
- }
- return mkPCastTo(mce, finalVtype, curr );
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Generating expensive sequences for exact carry-chain ---*/
-/*--- propagation in add/sub and related operations. ---*/
-/*------------------------------------------------------------*/
-
-static
-__attribute__((unused))
-IRAtom* expensiveAdd32 ( MCEnv* mce, IRAtom* qaa, IRAtom* qbb,
- IRAtom* aa, IRAtom* bb )
-{
- IRAtom *a_min, *b_min, *a_max, *b_max;
- IRType ty;
- IROp opAND, opOR, opXOR, opNOT, opADD;
-
- tl_assert(isShadowAtom(mce,qaa));
- tl_assert(isShadowAtom(mce,qbb));
- tl_assert(isOriginalAtom(mce,aa));
- tl_assert(isOriginalAtom(mce,bb));
- tl_assert(sameKindedAtoms(qaa,aa));
- tl_assert(sameKindedAtoms(qbb,bb));
-
- ty = Ity_I32;
- opAND = Iop_And32;
- opOR = Iop_Or32;
- opXOR = Iop_Xor32;
- opNOT = Iop_Not32;
- opADD = Iop_Add32;
-
- // a_min = aa & ~qaa
- a_min = assignNew(mce,ty,
- binop(opAND, aa,
- assignNew(mce,ty, unop(opNOT, qaa))));
-
- // b_min = bb & ~qbb
- b_min = assignNew(mce,ty,
- binop(opAND, bb,
- assignNew(mce,ty, unop(opNOT, qbb))));
-
- // a_max = aa | qaa
- a_max = assignNew(mce,ty, binop(opOR, aa, qaa));
-
- // b_max = bb | qbb
- b_max = assignNew(mce,ty, binop(opOR, bb, qbb));
-
- // result = (qaa | qbb) | ((a_min + b_min) ^ (a_max + b_max))
- return
- assignNew(mce,ty,
- binop( opOR,
- assignNew(mce,ty, binop(opOR, qaa, qbb)),
- assignNew(mce,ty,
- binop(opXOR, assignNew(mce,ty, binop(opADD, a_min, b_min)),
- assignNew(mce,ty, binop(opADD, a_max, b_max))
- )
- )
- )
- );
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Helpers for dealing with vector primops. ---*/
-/*------------------------------------------------------------*/
-
-/* Vector pessimisation -- pessimise within each lane individually. */
-
-static IRAtom* mkPCast8x16 ( MCEnv* mce, IRAtom* at )
-{
- return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ8x16, at));
-}
-
-static IRAtom* mkPCast16x8 ( MCEnv* mce, IRAtom* at )
-{
- return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ16x8, at));
-}
-
-static IRAtom* mkPCast32x4 ( MCEnv* mce, IRAtom* at )
-{
- return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ32x4, at));
-}
-
-static IRAtom* mkPCast64x2 ( MCEnv* mce, IRAtom* at )
-{
- return assignNew(mce, Ity_V128, unop(Iop_CmpNEZ64x2, at));
-}
-
-
-/* Here's a simple scheme capable of handling ops derived from SSE1
- code and while only generating ops that can be efficiently
- implemented in SSE1. */
-
-/* All-lanes versions are straightforward:
-
- binary32Fx4(x,y) ==> PCast32x4(UifUV128(x#,y#))
-
- unary32Fx4(x,y) ==> PCast32x4(x#)
-
- Lowest-lane-only versions are more complex:
-
- binary32F0x4(x,y) ==> SetV128lo32(
- x#,
- PCast32(V128to32(UifUV128(x#,y#)))
- )
-
- This is perhaps not so obvious. In particular, it's faster to
- do a V128-bit UifU and then take the bottom 32 bits than the more
- obvious scheme of taking the bottom 32 bits of each operand
- and doing a 32-bit UifU. Basically since UifU is fast and
- chopping lanes off vector values is slow.
-
- Finally:
-
- unary32F0x4(x) ==> SetV128lo32(
- x#,
- PCast32(V128to32(x#))
- )
-
- Where:
-
- PCast32(v#) = 1Sto32(CmpNE32(v#,0))
- PCast32x4(v#) = CmpNEZ32x4(v#)
-*/
-
-static
-IRAtom* binary32Fx4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- tl_assert(isShadowAtom(mce, vatomY));
- at = mkUifUV128(mce, vatomX, vatomY);
- at = assignNew(mce, Ity_V128, mkPCast32x4(mce, at));
- return at;
-}
-
-static
-IRAtom* unary32Fx4 ( MCEnv* mce, IRAtom* vatomX )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- at = assignNew(mce, Ity_V128, mkPCast32x4(mce, vatomX));
- return at;
-}
-
-static
-IRAtom* binary32F0x4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- tl_assert(isShadowAtom(mce, vatomY));
- at = mkUifUV128(mce, vatomX, vatomY);
- at = assignNew(mce, Ity_I32, unop(Iop_V128to32, at));
- at = mkPCastTo(mce, Ity_I32, at);
- at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at));
- return at;
-}
-
-static
-IRAtom* unary32F0x4 ( MCEnv* mce, IRAtom* vatomX )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- at = assignNew(mce, Ity_I32, unop(Iop_V128to32, vatomX));
- at = mkPCastTo(mce, Ity_I32, at);
- at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at));
- return at;
-}
-
-/* --- ... and ... 64Fx2 versions of the same ... --- */
-
-static
-IRAtom* binary64Fx2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- tl_assert(isShadowAtom(mce, vatomY));
- at = mkUifUV128(mce, vatomX, vatomY);
- at = assignNew(mce, Ity_V128, mkPCast64x2(mce, at));
- return at;
-}
-
-static
-IRAtom* unary64Fx2 ( MCEnv* mce, IRAtom* vatomX )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- at = assignNew(mce, Ity_V128, mkPCast64x2(mce, vatomX));
- return at;
-}
-
-static
-IRAtom* binary64F0x2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- tl_assert(isShadowAtom(mce, vatomY));
- at = mkUifUV128(mce, vatomX, vatomY);
- at = assignNew(mce, Ity_I64, unop(Iop_V128to64, at));
- at = mkPCastTo(mce, Ity_I64, at);
- at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at));
- return at;
-}
-
-static
-IRAtom* unary64F0x2 ( MCEnv* mce, IRAtom* vatomX )
-{
- IRAtom* at;
- tl_assert(isShadowAtom(mce, vatomX));
- at = assignNew(mce, Ity_I64, unop(Iop_V128to64, vatomX));
- at = mkPCastTo(mce, Ity_I64, at);
- at = assignNew(mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at));
- return at;
-}
-
-/* --- --- Vector saturated narrowing --- --- */
-
-/* This is quite subtle. What to do is simple:
-
- Let the original narrowing op be QNarrowW{S,U}xN. Produce:
-
- the-narrowing-op( PCastWxN(vatom1), PCastWxN(vatom2))
-
- Why this is right is not so simple. Consider a lane in the args,
- vatom1 or 2, doesn't matter.
-
- After the PCast, that lane is all 0s (defined) or all
- 1s(undefined).
-
- Both signed and unsigned saturating narrowing of all 0s produces
- all 0s, which is what we want.
-
- The all-1s case is more complex. Unsigned narrowing interprets an
- all-1s input as the largest unsigned integer, and so produces all
- 1s as a result since that is the largest unsigned value at the
- smaller width.
-
- Signed narrowing interprets all 1s as -1. Fortunately, -1 narrows
- to -1, so we still wind up with all 1s at the smaller width.
-
- So: In short, pessimise the args, then apply the original narrowing
- op.
-*/
-static
-IRAtom* vectorNarrowV128 ( MCEnv* mce, IROp narrow_op,
- IRAtom* vatom1, IRAtom* vatom2)
-{
- IRAtom *at1, *at2, *at3;
- IRAtom* (*pcast)( MCEnv*, IRAtom* );
- switch (narrow_op) {
- case Iop_QNarrowBin32Sto16Sx8: pcast = mkPCast32x4; break;
- case Iop_QNarrowBin16Sto8Sx16: pcast = mkPCast16x8; break;
- case Iop_QNarrowBin16Sto8Ux16: pcast = mkPCast16x8; break;
- default: VG_(tool_panic)("vectorNarrowV128");
- }
- tl_assert(isShadowAtom(mce,vatom1));
- tl_assert(isShadowAtom(mce,vatom2));
- at1 = assignNew(mce, Ity_V128, pcast(mce, vatom1));
- at2 = assignNew(mce, Ity_V128, pcast(mce, vatom2));
- at3 = assignNew(mce, Ity_V128, binop(narrow_op, at1, at2));
- return at3;
-}
-
-
-/* --- --- Vector integer arithmetic --- --- */
-
-/* Simple ... UifU the args and per-lane pessimise the results. */
-static
-IRAtom* binary8Ix16 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 )
-{
- IRAtom* at;
- at = mkUifUV128(mce, vatom1, vatom2);
- at = mkPCast8x16(mce, at);
- return at;
-}
-
-static
-IRAtom* binary16Ix8 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 )
-{
- IRAtom* at;
- at = mkUifUV128(mce, vatom1, vatom2);
- at = mkPCast16x8(mce, at);
- return at;
-}
-
-static
-IRAtom* binary32Ix4 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 )
-{
- IRAtom* at;
- at = mkUifUV128(mce, vatom1, vatom2);
- at = mkPCast32x4(mce, at);
- return at;
-}
-
-static
-IRAtom* binary64Ix2 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 )
-{
- IRAtom* at;
- at = mkUifUV128(mce, vatom1, vatom2);
- at = mkPCast64x2(mce, at);
- return at;
-}
-
-
-/*------------------------------------------------------------*/
-/*--- Generate shadow values from all kinds of IRExprs. ---*/
-/*------------------------------------------------------------*/
-
-static
-IRAtom* expr2vbits_Binop ( MCEnv* mce,
- IROp op,
- IRAtom* atom1, IRAtom* atom2 )
-{
- IRType and_or_ty;
- IRAtom* (*uifu) (MCEnv*, IRAtom*, IRAtom*);
- IRAtom* (*difd) (MCEnv*, IRAtom*, IRAtom*);
- IRAtom* (*improve) (MCEnv*, IRAtom*, IRAtom*);
-
- IRAtom* vatom1 = expr2vbits( mce, atom1 );
- IRAtom* vatom2 = expr2vbits( mce, atom2 );
-
- tl_assert(isOriginalAtom(mce,atom1));
- tl_assert(isOriginalAtom(mce,atom2));
- tl_assert(isShadowAtom(mce,vatom1));
- tl_assert(isShadowAtom(mce,vatom2));
- tl_assert(sameKindedAtoms(atom1,vatom1));
- tl_assert(sameKindedAtoms(atom2,vatom2));
- switch (op) {
-
- /* V128-bit SIMD (SSE2-esque) */
-
- case Iop_ShrN16x8:
- case Iop_ShrN32x4:
- case Iop_ShrN64x2:
- case Iop_SarN16x8:
- case Iop_SarN32x4:
- case Iop_ShlN16x8:
- case Iop_ShlN32x4:
- case Iop_ShlN64x2:
- /* Same scheme as with all other shifts. */
- complainIfUndefined(mce, atom2);
- return assignNew(mce, Ity_V128, binop(op, vatom1, atom2));
-
- case Iop_QSub8Ux16:
- case Iop_QSub8Sx16:
- case Iop_Sub8x16:
- case Iop_Min8Ux16:
- case Iop_Max8Ux16:
- case Iop_CmpGT8Sx16:
- case Iop_CmpEQ8x16:
- case Iop_Avg8Ux16:
- case Iop_QAdd8Ux16:
- case Iop_QAdd8Sx16:
- case Iop_Add8x16:
- return binary8Ix16(mce, vatom1, vatom2);
-
- case Iop_QSub16Ux8:
- case Iop_QSub16Sx8:
- case Iop_Sub16x8:
- case Iop_Mul16x8:
- case Iop_MulHi16Sx8:
- case Iop_MulHi16Ux8:
- case Iop_Min16Sx8:
- case Iop_Max16Sx8:
- case Iop_CmpGT16Sx8:
- case Iop_CmpEQ16x8:
- case Iop_Avg16Ux8:
- case Iop_QAdd16Ux8:
- case Iop_QAdd16Sx8:
- case Iop_Add16x8:
- return binary16Ix8(mce, vatom1, vatom2);
-
- case Iop_Sub32x4:
- case Iop_QSub32Sx4:
- case Iop_QSub32Ux4:
- case Iop_CmpGT32Sx4:
- case Iop_CmpEQ32x4:
- case Iop_Add32x4:
- case Iop_QAdd32Ux4:
- case Iop_QAdd32Sx4:
- return binary32Ix4(mce, vatom1, vatom2);
-
- case Iop_Sub64x2:
- case Iop_QSub64Ux2:
- case Iop_QSub64Sx2:
- case Iop_Add64x2:
- case Iop_QAdd64Ux2:
- case Iop_QAdd64Sx2:
- return binary64Ix2(mce, vatom1, vatom2);
-
- case Iop_QNarrowBin32Sto16Sx8:
- case Iop_QNarrowBin16Sto8Sx16:
- case Iop_QNarrowBin16Sto8Ux16:
- return vectorNarrowV128(mce, op, vatom1, vatom2);
-
- case Iop_Sub64Fx2:
- case Iop_Mul64Fx2:
- case Iop_Min64Fx2:
- case Iop_Max64Fx2:
- case Iop_Div64Fx2:
- case Iop_CmpLT64Fx2:
- case Iop_CmpLE64Fx2:
- case Iop_CmpEQ64Fx2:
- case Iop_Add64Fx2:
- return binary64Fx2(mce, vatom1, vatom2);
-
- case Iop_Sub64F0x2:
- case Iop_Mul64F0x2:
- case Iop_Min64F0x2:
- case Iop_Max64F0x2:
- case Iop_Div64F0x2:
- case Iop_CmpLT64F0x2:
- case Iop_CmpLE64F0x2...
[truncated message content] |