From 7dd47998f00712515c25fb852b6c0cf958120508 Mon Sep 17 00:00:00 2001 From: Coleen Phillimore Date: Mon, 24 Jul 2023 12:09:27 +0000 Subject: [PATCH] 8311847: Fix -Wconversion for assembler.hpp emit_int8,16 callers Reviewed-by: dlong, aph --- src/hotspot/cpu/aarch64/assembler_aarch64.hpp | 18 +++++------ src/hotspot/cpu/x86/assembler_x86.cpp | 22 ++++++------- src/hotspot/share/asm/assembler.cpp | 1 - src/hotspot/share/asm/assembler.hpp | 31 ++++++++++++++++--- .../share/compiler/abstractDisassembler.cpp | 4 +-- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/hotspot/cpu/aarch64/assembler_aarch64.hpp b/src/hotspot/cpu/aarch64/assembler_aarch64.hpp index 7ea4d523996..77508e4528e 100644 --- a/src/hotspot/cpu/aarch64/assembler_aarch64.hpp +++ b/src/hotspot/cpu/aarch64/assembler_aarch64.hpp @@ -221,7 +221,7 @@ public: mask <<= lsb; unsigned target = *(unsigned *)a; target &= ~mask; - target |= val; + target |= (unsigned)val; *(unsigned *)a = target; } @@ -229,14 +229,14 @@ public: int nbits = msb - lsb + 1; int64_t chk = val >> (nbits - 1); guarantee (chk == -1 || chk == 0, "Field too big for insn at " INTPTR_FORMAT, p2i(a)); - unsigned uval = val; + uint64_t uval = val; unsigned mask = checked_cast(right_n_bits(nbits)); uval &= mask; uval <<= lsb; mask <<= lsb; unsigned target = *(unsigned *)a; target &= ~mask; - target |= uval; + target |= (unsigned)uval; *(unsigned *)a = target; } @@ -262,10 +262,10 @@ public: int nbits = msb - lsb + 1; int64_t chk = val >> (nbits - 1); guarantee (chk == -1 || chk == 0, "Field too big for insn"); - unsigned uval = val; + uint64_t uval = val; unsigned mask = checked_cast(right_n_bits(nbits)); uval &= mask; - f(uval, lsb + nbits - 1, lsb); + f((unsigned)uval, lsb + nbits - 1, lsb); } void rf(Register r, int lsb) { @@ -553,7 +553,7 @@ class Address { i->sf(offset(), 20, 12); } else { i->f(0b01, 25, 24); - i->f(offset() >> size, 21, 10); + i->f(checked_cast(offset() >> size), 21, 10); } } break; @@ -653,7 +653,7 @@ class Address { static bool offset_ok_for_sve_immed(int64_t offset, int shift, int vl /* sve vector length */) { if (offset % vl == 0) { // Convert address offset into sve imm offset (MUL VL). - int sve_offset = offset / vl; + int64_t sve_offset = offset / vl; if (((-(1 << (shift - 1))) <= sve_offset) && (sve_offset < (1 << (shift - 1)))) { // sve_offset can be encoded return true; @@ -2412,7 +2412,7 @@ public: ld_st(Vt, T, a.base(), op1, op2); break; case Address::post: - ld_st(Vt, T, a.base(), a.offset(), op1, op2, regs); + ld_st(Vt, T, a.base(), checked_cast(a.offset()), op1, op2, regs); break; case Address::post_reg: ld_st(Vt, T, a.base(), a.index(), op1, op2); @@ -3522,7 +3522,7 @@ private: int op1, int type, int imm_op2, int scalar_op2) { switch (a.getMode()) { case Address::base_plus_offset: - sve_ld_st1(Zt, a.base(), a.offset(), Pg, T, op1, type, imm_op2); + sve_ld_st1(Zt, a.base(), checked_cast(a.offset()), Pg, T, op1, type, imm_op2); break; case Address::base_plus_offset_reg: sve_ld_st1(Zt, a.base(), a.index(), Pg, T, op1, type, scalar_op2); diff --git a/src/hotspot/cpu/x86/assembler_x86.cpp b/src/hotspot/cpu/x86/assembler_x86.cpp index d2a350911e5..5d6335691bc 100644 --- a/src/hotspot/cpu/x86/assembler_x86.cpp +++ b/src/hotspot/cpu/x86/assembler_x86.cpp @@ -26,9 +26,9 @@ #include "asm/assembler.hpp" #include "asm/assembler.inline.hpp" #include "gc/shared/cardTableBarrierSet.hpp" -#include "gc/shared/collectedHeap.inline.hpp" #include "interpreter/interpreter.hpp" #include "memory/resourceArea.hpp" +#include "memory/universe.hpp" #include "prims/methodHandles.hpp" #include "runtime/objectMonitor.hpp" #include "runtime/os.hpp" @@ -2434,7 +2434,7 @@ void Assembler::jcc(Condition cc, Label& L, bool maybe_short) { const int short_size = 2; const int long_size = 6; - intptr_t offs = (intptr_t)dst - (intptr_t)pc(); + int offs = checked_cast((intptr_t)dst - (intptr_t)pc()); if (maybe_short && is8bit(offs - short_size)) { // 0111 tttn #8-bit disp emit_int16(0x70 | cc, (offs - short_size) & 0xFF); @@ -2461,14 +2461,14 @@ void Assembler::jccb_0(Condition cc, Label& L, const char* file, int line) { const int short_size = 2; address entry = target(L); #ifdef ASSERT - intptr_t dist = (intptr_t)entry - ((intptr_t)pc() + short_size); - intptr_t delta = short_branch_delta(); + int dist = checked_cast((intptr_t)entry - (intptr_t)(pc() + short_size)); + int delta = short_branch_delta(); if (delta != 0) { dist += (dist < 0 ? (-delta) :delta); } - assert(is8bit(dist), "Dispacement too large for a short jmp at %s:%d", file, line); + assert(is8bit(dist), "Displacement too large for a short jmp at %s:%d", file, line); #endif - intptr_t offs = (intptr_t)entry - (intptr_t)pc(); + int offs = checked_cast((intptr_t)entry - (intptr_t)pc()); // 0111 tttn #8-bit disp emit_int16(0x70 | cc, (offs - short_size) & 0xFF); } else { @@ -2492,7 +2492,7 @@ void Assembler::jmp(Label& L, bool maybe_short) { InstructionMark im(this); const int short_size = 2; const int long_size = 5; - intptr_t offs = entry - pc(); + int offs = checked_cast(entry - pc()); if (maybe_short && is8bit(offs - short_size)) { emit_int16((unsigned char)0xEB, ((offs - short_size) & 0xFF)); } else { @@ -2531,12 +2531,12 @@ void Assembler::jmpb_0(Label& L, const char* file, int line) { address entry = target(L); assert(entry != nullptr, "jmp most probably wrong"); #ifdef ASSERT - intptr_t dist = (intptr_t)entry - ((intptr_t)pc() + short_size); - intptr_t delta = short_branch_delta(); + int dist = checked_cast((intptr_t)entry - (intptr_t)(pc() + short_size)); + int delta = short_branch_delta(); if (delta != 0) { dist += (dist < 0 ? (-delta) :delta); } - assert(is8bit(dist), "Dispacement too large for a short jmp at %s:%d", file, line); + assert(is8bit(dist), "Displacement too large for a short jmp at %s:%d", file, line); #endif intptr_t offs = entry - pc(); emit_int16((unsigned char)0xEB, (offs - short_size) & 0xFF); @@ -6387,7 +6387,7 @@ void Assembler::xbegin(Label& abort, relocInfo::relocType rtype) { if (abort.is_bound()) { address entry = target(abort); assert(entry != nullptr, "abort entry null"); - intptr_t offset = entry - pc(); + int offset = checked_cast(entry - pc()); emit_int16((unsigned char)0xC7, (unsigned char)0xF8); emit_int32(offset - 6); // 2 opcode + 4 address } else { diff --git a/src/hotspot/share/asm/assembler.cpp b/src/hotspot/share/asm/assembler.cpp index 4c7715e975c..f65890860da 100644 --- a/src/hotspot/share/asm/assembler.cpp +++ b/src/hotspot/share/asm/assembler.cpp @@ -33,7 +33,6 @@ #include "runtime/javaThread.hpp" #include "runtime/os.hpp" - // Implementation of AbstractAssembler // // The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster, diff --git a/src/hotspot/share/asm/assembler.hpp b/src/hotspot/share/asm/assembler.hpp index add473999ea..e38b87ff509 100644 --- a/src/hotspot/share/asm/assembler.hpp +++ b/src/hotspot/share/asm/assembler.hpp @@ -290,6 +290,20 @@ class AbstractAssembler : public ResourceObj { }; #endif + // sign-extended tolerant cast needed by callers of emit_int8 and emit_int16 + // Some callers pass signed types that need to fit into the unsigned type so check + // that the range is correct. + template + constexpr T narrow_cast(int x) const { + if (x < 0) { + using stype = std::make_signed_t; + assert(x >= std::numeric_limits::min(), "too negative"); // >= -128 for 8 bits + return static_cast(x); // cut off sign bits + } else { + return checked_cast(x); + } + } + public: // Creation @@ -298,15 +312,22 @@ class AbstractAssembler : public ResourceObj { // ensure buf contains all code (call this before using/copying the code) void flush(); - void emit_int8( uint8_t x1) { code_section()->emit_int8(x1); } + void emit_int8( int x1) { code_section()->emit_int8(narrow_cast(x1)); } - void emit_int16( uint16_t x) { code_section()->emit_int16(x); } - void emit_int16( uint8_t x1, uint8_t x2) { code_section()->emit_int16(x1, x2); } + void emit_int16( int x) { code_section()->emit_int16(narrow_cast(x)); } - void emit_int24( uint8_t x1, uint8_t x2, uint8_t x3) { code_section()->emit_int24(x1, x2, x3); } + void emit_int16( int x1, int x2) { code_section()->emit_int16(narrow_cast(x1), + narrow_cast(x2)); } + + void emit_int24( int x1, int x2, int x3) { code_section()->emit_int24(narrow_cast(x1), + narrow_cast(x2), + narrow_cast(x3)); } void emit_int32( uint32_t x) { code_section()->emit_int32(x); } - void emit_int32( uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4) { code_section()->emit_int32(x1, x2, x3, x4); } + void emit_int32( int x1, int x2, int x3, int x4) { code_section()->emit_int32(narrow_cast(x1), + narrow_cast(x2), + narrow_cast(x3), + narrow_cast(x4)); } void emit_int64( uint64_t x) { code_section()->emit_int64(x); } diff --git a/src/hotspot/share/compiler/abstractDisassembler.cpp b/src/hotspot/share/compiler/abstractDisassembler.cpp index 96611f9f60d..6117108155f 100644 --- a/src/hotspot/share/compiler/abstractDisassembler.cpp +++ b/src/hotspot/share/compiler/abstractDisassembler.cpp @@ -75,8 +75,8 @@ int AbstractDisassembler::print_location(address here, address begin, address en if ((uintptr_t)end < (uintptr_t)here) st->print(">> end(" PTR_FORMAT ") < here(" PTR_FORMAT ")<<", p2i(end), p2i(here)); assert((uintptr_t)begin <= (uintptr_t)end, "inverted address range"); #endif - const int blob_len = end - begin; - const int offset = here - begin; + const int blob_len = pointer_delta_as_int(end, begin); + const int offset = pointer_delta_as_int(here, begin); const int width = (blob_len < (1<< 8)) ? 2 : (blob_len < (1<<16)) ? 4 : (blob_len < (1<<24)) ? 6 : 8; if (print_header) { st->print(" %*s", width+5, "offset");