From a00c960da2f994173d5acd2fadded2b5ae6f5b97 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 26 Jan 2004 17:14:17 +0000 Subject: [PATCH] Made code more concise. In particular: - introduced DIS() and DIP() macros to shorten debug printing - introduce jmp_lit(), jcc_lit(), jmp_treg() for common UCode sequences - replace many unnecessary dis?dis_buf:NULL tests with dis_buf, by changing the tests in disAMode() Overall, reduced code size by about 230 lines. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@2234 --- coregrind/vg_to_ucode.c | 1247 ++++++++++++++++----------------------- 1 file changed, 504 insertions(+), 743 deletions(-) diff --git a/coregrind/vg_to_ucode.c b/coregrind/vg_to_ucode.c index d62459ddf..2a6b49405 100644 --- a/coregrind/vg_to_ucode.c +++ b/coregrind/vg_to_ucode.c @@ -33,11 +33,16 @@ /*------------------------------------------------------------*/ -/*--- Renamings of frequently-used global functions. ---*/ +/*--- Debugging output ---*/ /*------------------------------------------------------------*/ -#define dis VG_(print_codegen) +#define DIP(format, args...) \ + if (VG_(print_codegen)) \ + VG_(printf)(format, ## args) +#define DIS(buf, format, args...) \ + if (VG_(print_codegen)) \ + VG_(sprintf)(buf, format, ## args) /*------------------------------------------------------------*/ /*--- CPU feature set stuff ---*/ @@ -349,7 +354,6 @@ __inline__ static UInt getSDisp ( Int size, Addr eip ) return 0; /*notreached*/ } - /*------------------------------------------------------------*/ /*--- Flag-related helpers. ---*/ /*------------------------------------------------------------*/ @@ -388,6 +392,34 @@ void VG_(set_cond_field) ( UCodeBlock* cb, Condcode cond ) LAST_UINSTR(cb).cond = cond; } +/*------------------------------------------------------------*/ +/*--- JMP helpers ---*/ +/*------------------------------------------------------------*/ + +static __inline__ +void jmp_lit( UCodeBlock* cb, Addr d32 ) +{ + uInstr1 (cb, JMP, 0, Literal, 0); + uLiteral(cb, d32); + uCond (cb, CondAlways); +} + +static __inline__ +void jmp_treg( UCodeBlock* cb, Int t ) +{ + uInstr1 (cb, JMP, 0, TempReg, t); + uCond (cb, CondAlways); +} + +static __inline__ +void jcc_lit( UCodeBlock* cb, Addr d32, Condcode cond ) +{ + uInstr1 (cb, JMP, 0, Literal, 0); + uLiteral (cb, d32); + uCond (cb, cond); + uFlagsRWU(cb, FlagsOSZACP, FlagsEmpty, FlagsEmpty); +} + /*------------------------------------------------------------*/ /*--- Disassembling addressing modes ---*/ @@ -442,8 +474,8 @@ void handleSegOverride ( UCodeBlock* cb, UChar sorb, Int tmp ) temporary, and the number of bytes in the address mode, are returned, as a pair (length << 24) | temp. Note that this fn should not be called if the R/M part of the address denotes a register - instead of memory. If buf is non-NULL, text of the addressing mode - is placed therein. */ + instead of memory. If VG_(print_codegen) is true, text of the addressing + mode is placed therein. */ static UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) @@ -468,8 +500,7 @@ UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) { UChar rm = mod_reg_rm; uInstr2(cb, GET, 4, ArchReg, rm, TempReg, tmp); handleSegOverride(cb, sorb, tmp); - if (buf) VG_(sprintf)(buf,"%s(%s)", sorbTxt(sorb), - nameIReg(4,rm)); + DIS(buf, "%s(%s)", sorbTxt(sorb), nameIReg(4,rm)); return (1<<24 | tmp); } @@ -483,10 +514,9 @@ UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) UInt d = getSDisp8((Addr)eip); eip++; uInstr2(cb, GET, 4, ArchReg, rm, TempReg, tmq); uInstr2(cb, LEA1, 4, TempReg, tmq, TempReg, tmp); - LAST_UINSTR(cb).lit32 = d; + uLiteral(cb, d); handleSegOverride(cb, sorb, tmp); - if (buf) VG_(sprintf)(buf,"%s%d(%s)", sorbTxt(sorb), - d, nameIReg(4,rm)); + DIS(buf, "%s%d(%s)", sorbTxt(sorb), d, nameIReg(4,rm)); return (2<<24 | tmp); } @@ -500,10 +530,9 @@ UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) UInt d = getUDisp32((Addr)eip); eip += 4; uInstr2(cb, GET, 4, ArchReg, rm, TempReg, tmq); uInstr2(cb, LEA1, 4, TempReg, tmq, TempReg, tmp); - LAST_UINSTR(cb).lit32 = d; + uLiteral(cb, d); handleSegOverride(cb, sorb, tmp); - if (buf) VG_(sprintf)(buf,"%s0x%x(%s)", sorbTxt(sorb), - d, nameIReg(4,rm)); + DIS(buf, "%s0x%x(%s)", sorbTxt(sorb), d, nameIReg(4,rm)); return (5<<24 | tmp); } @@ -520,7 +549,7 @@ UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) uInstr2(cb, MOV, 4, Literal, 0, TempReg, tmp); uLiteral(cb, d); handleSegOverride(cb, sorb, tmp); - if (buf) VG_(sprintf)(buf,"%s(0x%x)", sorbTxt(sorb), d); + DIS(buf, "%s(0x%x)", sorbTxt(sorb), d); return (5<<24 | tmp); } @@ -556,12 +585,11 @@ UInt disAMode ( UCodeBlock* cb, UChar sorb, Addr eip0, UChar* buf ) uInstr2(cb, GET, 4, ArchReg, base_r, TempReg, base_tmp); uInstr3(cb, LEA2, 4, TempReg, base_tmp, TempReg, index_tmp, TempReg, tmp); - LAST_UINSTR(cb).lit32 = 0; + uLiteral(cb, 0); LAST_UINSTR(cb).extra4b = 1 << scale; handleSegOverride(cb, sorb, tmp); - if (buf) VG_(sprintf)(buf,"%s(%s,%s,%d)", sorbTxt(sorb), - nameIReg(4,base_r), - nameIReg(4,index_r),1<used; first_uinstr++) { Bool sane = VG_(saneUInstr)(True, True, &cb->instrs[first_uinstr]); - if (dis) + if (VG_(print_codegen)) VG_(pp_UInstr)(first_uinstr, &cb->instrs[first_uinstr]); else if (!sane) VG_(up_UInstr)(-1, &cb->instrs[first_uinstr]); @@ -7259,7 +7020,7 @@ Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 ) Bool block_sane; Int delta = 0; - if (dis) VG_(printf)("Original x86 code to UCode:\n\n"); + DIP("Original x86 code to UCode:\n\n"); /* After every x86 instruction do an INCEIP, except for the final one * in the basic block. For them we patch in the x86 instruction size @@ -7275,13 +7036,12 @@ Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 ) * already end with a JMP instr. We also need to check for no UCode, * which occurs if the x86 instr was a nop */ if (cb->used == 0 || LAST_UINSTR(cb).opcode != JMP) { - uInstr1(cb, JMP, 0, Literal, 0); - uLiteral(cb, eip); - uCond(cb, CondAlways); + jmp_lit(cb, eip); /* Print added JMP */ - if (dis) VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); + if (VG_(print_codegen)) + VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); } - if (dis) VG_(printf)("\n"); + DIP("\n"); delta = eip - eip0; } else { @@ -7297,19 +7057,19 @@ Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 ) VG_(message)(Vg_DebugMsg, "Warning: splitting giant basic block into pieces at %p %(y", eip, eip); - uInstr1(cb, JMP, 0, Literal, 0); - uLiteral(cb, eip); - uCond(cb, CondAlways); + jmp_lit(cb, eip); /* Print added JMP */ - if (dis) VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); + if (VG_(print_codegen)) + VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); isEnd = True; } else if (!isEnd) { uInstr1(cb, INCEIP, 0, Lit16, delta); /* Print added INCEIP */ - if (dis) VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); + if (VG_(print_codegen)) + VG_(pp_UInstr)(cb->used-1, &cb->instrs[cb->used-1]); } - if (dis) VG_(printf)("\n"); + DIP("\n"); } } @@ -7325,7 +7085,8 @@ Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 ) return eip - eip0; } -#undef dis +#undef DIP +#undef DIS /*--------------------------------------------------------------------*/ /*--- end vg_to_ucode.c ---*/