Index: lib/ubsan/ubsan_value.cc =================================================================== --- lib/ubsan/ubsan_value.cc +++ lib/ubsan/ubsan_value.cc @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +#include #include "ubsan_platform.h" #if CAN_SANITIZE_UB #include "ubsan_value.h" @@ -27,7 +28,24 @@ // to SIntMax. const unsigned ExtraBits = sizeof(SIntMax) * 8 - getType().getIntegerBitWidth(); +#if defined(__mips__) + UIntMax Value = UIntMax(Val) << ExtraBits >> ExtraBits;; + uptr ExtraBytes= ExtraBits / 8; +#if BYTE_ORDER == LITTLE_ENDIAN + u8 * ValuePtr = (u8 *) &Value + (sizeof(UIntMax) - ExtraBytes); +#else + u8 * ValuePtr = (u8 *) &Value; +#endif + uptr Mask = 1UL << (getType().getIntegerBitWidth() - 1); + if(Val & Mask) { + for (uptr i = 0 ; i < ExtraBytes ; i++) { + *(ValuePtr + i) |= 0xff; + } + } + return SIntMax(Value); +#else return SIntMax(Val) << ExtraBits >> ExtraBits; +#endif } if (getType().getIntegerBitWidth() == 64) return *reinterpret_cast(Val);